MySQL Database Connection in Python

Pradeep Elance
Updated on 04-Feb-2020 06:44:47

311 Views

Mysql is one of the most widely used open source Dbs. Python provides ways to connect to this DB and use the DB to store and retrive the data from it.Install pymysqlDepending on the python environment you are using, pymysql package can be installed using one of the following methods.# From python console pip install pymysql #Using Anaconda conda install -c anaconda pymysql # Add modules using any python IDE pymysqlConnecting to MySqlNow we can connect to the Mysql environment using the following code. After connecting we are finding out the version of the DB.Exampleimport pymysql # Open database connection ... Read More

Maximum Length of Consecutive 1's in a Binary String in Python Using Map Function

Pradeep Elance
Updated on 04-Feb-2020 06:41:11

465 Views

Sometimes when dealing with the binary representation of numbers we may be needed to find out how many continuous 1’s are present in the number. This article shows two ways how we can find out that.Using Split and MapThe split function in python can be used to split the given string into multiple strings. We split it by zeros and the map function is used to find the maximum length among the splits generated.Example Live Demodata = '11110000111110000011111010101010101011111111' def Max_len_cons_1(data): print ("Maximum Number of consecutive one's: ", max(map(len, data.split('0'))) ) Max_len_cons_1(data)OutputRunning the above code gives us the following result −Maximum Number ... Read More

Usage of Asterisks in Python

Pradeep Elance
Updated on 04-Feb-2020 06:29:16

288 Views

Python programming language uses both * and ** on different contexts. In this article we will see how these two are used and what the respective useful scenarios.As an Infix OperatorWhen * is used as infix operator, it basically gives the mathematical product of numbers. IN the below example we take integers. Floats and complex numbers to multiply and get the results.Example Live Demo# Integers x = 20 y = 10 z = x * y print(z, "") # Floats x1 = 2.5 y1 = 5.1 z1 = x1 * y1 print(z1, "") # Complex Numbers x2 = 4 ... Read More

Statistical Thinking in Python

Pradeep Elance
Updated on 04-Feb-2020 06:22:13

394 Views

Statistics is fundamental to learn ml and AI. As Python is the language of choice for these Technologies, we will see how to write Python programs which incorporate statistical analysis. In this article we will see how to create graphs and charts using various Python modules. This variety of charts help us in analyzing the data quickly and deriving insides are conclusions graphically.Data PreparationWe take the data set containing the data about various seeds. This data set is available at kaggle in the link shown in the program below. It has eight columns which will be used to cerate various ... Read More

Use of Escape Character in Text File for MySQL Import

Prabhas
Updated on 04-Feb-2020 06:14:32

392 Views

Use of escape character (\) would become very essential when we want to insert a comma or any other character between the values of a filed. It can be understood with the help of an example. Suppose we want to import the data from a text file named A.txt, having the following data, into a MySQL table −id,   Name,    Country,       Salary 105,  Chum,    Marsh, USA,     11000 106,  Danny,   Harrison, AUS,  12000Here, we can see that the filed name has two values first name, last name separated by a comma. Now, the ... Read More

Python for Spreadsheet Users

Pradeep Elance
Updated on 04-Feb-2020 06:12:44

244 Views

Excel is the most famous spreadsheet and almost every computer user is comfortable with the idea of managing the data through spreadsheets. Eventually some python program has to interact with excel. Many python libraries are available to create, read and write into excel files. We will see the examples of few such important libraries below.Using openpyxlThis library can read/write Excel 2010 xlsx/xlsm/xltx/xltm files. In the below example we create a excel worksheet, assign data to its cells and finally save the file to a desired location. The module has many in-built methods which can be used for this. We see ... Read More

MySQL Evaluation of Escape Characters in Text File Imports

Srinivas Gorla
Updated on 04-Feb-2020 06:12:01

342 Views

Back-slash(\) is the by default escape character for the MySQL and when we use it in the text file then we do not need to mention it in the query while importing the data from text file to table. But if we use any other character as escape character then it must be mentioned by using ESCAPED BY option in the query while importing the text file into a table. It can be understood with the help of the following example −Suppose we are using star symbol (‘* ‘) as the escape character in a text file as follows −id, ... Read More

Use MySQL INSTR Function with WHERE Clause

Chandu yadav
Updated on 04-Feb-2020 06:10:29

1K+ Views

When we use INSTR() function with MySQL WHERE clause, we need to provide column name of the table as the first argument and the substring as second argument along with a comparison operator. Following is an example using ‘Student’ table to demonstrate it −ExampleSuppose we have the following values in ‘Student’ table −mysql> Select * from Student; +------+---------+---------+-----------+ | Id   | Name    | Address | Subject   | +------+---------+---------+-----------+ | 1    | Gaurav  | Delhi   | Computers | | 2    | Aarav   | Mumbai  | History   | | 15   | Harshit | ... Read More

Import Text File Data into MySQL Table

varun
Updated on 04-Feb-2020 06:08:24

304 Views

Actually, we can write the data on the same line in the text file by using a separator. In this case, while importing this text file into MySQL table then we must have to use ‘LINES TERMINATED BY’ option. It can be understood with the help of the following example −Suppose we are using ‘|’ as the LINE TERMINATOR symbol in a text file as follows −id, Name, Country, Salary|105, Chum*, Marsh, USA, 11000|106, Danny*, Harrison, AUS, 12000Now while importing this text file into MySQL table then we need to mention ‘LINE TERMINATED BY’ option also in the query as ... Read More

Predicting Customer Churn in Python

Pradeep Elance
Updated on 04-Feb-2020 06:07:21

626 Views

Every business depends on customer's loyalty. The repeat business from customer is one of the cornerstone for business profitability. So it is important to know the reason of customers leaving a business. Customers going away is known as customer churn. By looking at the past trends we can judge what factors influence customer churn and how to predict if a particular customer will go away from the business. In this article we will use ML algorithm to study the past trends in customer churn and then judge which customers are likely to churn.Data PreparationAs an example will consider the Telecom ... Read More

Advertisements