0% found this document useful (0 votes)
11 views101 pages

Python

This document provides an overview of data handling using the Pandas library in Python, focusing on its data structures such as Series and DataFrame. It explains the advantages of using Pandas, how to create Series and DataFrames, and various operations like selection, slicing, and mathematical operations. Additionally, it covers methods for adding, renaming, and deleting columns in DataFrames.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views101 pages

Python

This document provides an overview of data handling using the Pandas library in Python, focusing on its data structures such as Series and DataFrame. It explains the advantages of using Pandas, how to create Series and DataFrames, and various operations like selection, slicing, and mathematical operations. Additionally, it covers methods for adding, renaming, and deleting columns in DataFrames.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Visit Python4csip.

com for more updates

CHAPTER-1 Data Handling using Pandas –I

Pandas:
• It is a package useful for data analysis and manipulation.
• Pandas provide an easy way to create, manipulate and wrangle the
data.
• Pandas provide powerful and easy-to-use data structures, as well
as the means to quickly perform operations on these structures.

Data scientists use Pandas for its following advantages:

• Easily handles missing data.


• It uses Series for one-dimensional data structure and DataFrame
for multi-dimensional data structure.
• It provides an efficient way to slice the data.
• It provides a flexible way to merge, concatenate or reshape the
data.

DATA STRUCTURE IN PANDAS


A data structure is a way to arrange the data in such a way that so it
can be accessed quickly and we can perform various operation on this
data like- retrieval, deletion, modification etc.

Pandas deals with 3 data structure-

1. Series
2. Data Frame
3. Panel

We are having only series and data frame in our syllabus.

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit [Link] for more updates
Series
Series-Series is a one-dimensional
DATAFEAME array like structure with
homogeneous data, which can be used to handle and manipulate data.
What makes it special is its index attribute, which has incredible
functionality and is heavily mutable.

It has two parts-


1. Data part (An array of actual data)
2. Associated index with data (associated array of indexes or data labels)

e.g.-

Index Data

0 10

1 15

2 18

3 22

✓ We can say that Series is a labeled one-dimensional array


which can hold any type of data.
✓ Data of Series is always mutable, means it can be changed.
✓ But the size of Data of Series is always immutable, means it
cannot be changed.
✓ Series may be considered as a Data Structure with two
arrays out which one array works as Index (Labels) and the
second array works as original Data.
✓ Row Labels in Series are called Index.

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit [Link] for more updates
Syntax to create a Series:

<Series Object>=[Link] (data, index=idx (optional))

✓ Where data may be python sequence (Lists), ndarray,


scalar value or a python dictionary.

How to create Series with nd array


DATAFEAME
Program-

import pandas as pd
Output-
import numpy as np Default Index
0 10
arr=[Link]([10,15,18,22])
1 15
s = [Link](arr) 2 18

print(s) 3 22

Data
Here we create an
array of 4 values.

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit [Link] for more updates

How to create Series with Mutable index


DATAFEAME
Program-

import pandas as pd Output-


import numpy as np first a
arr=[Link](['a','b','c','d']) second b
third c
s=[Link](arr,
fourth d
index=['first','second','third','fourth'])

print(s)

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit [Link] for more updates

Creating a series from Scalar value

To create a series from scalar value, an index must be provided. The


scalar value will be repeated as per the length of index.

Creating a series from a Dictionary

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit [Link] for more updates

Mathematical Operations in Series

Print all the values of the Series by multiplying them by 2.

Print Square of all the values of the series.

Print all the values of the Series that are greater than 2.

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit [Link] for more updates

Example-2

While adding two series, if Non-Matching Index is found in either of the


Series, Then NaN will be printed corresponds to Non-Matching Index.
is

If Non-Matching Index is found in either of the series, then this Non-


Matching Index corresponding value of that series will be filled as 0.
is

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit [Link] for more updates

Head and Tail Functions in Series

head (): It is used to access the first 5 rows of a series.


Note :To access first 3 rows we can call series_name.head(3)

Result of [Link]()

Result of [Link](3)

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit [Link] for more updates

tail(): It is used to access the last 5 rows of a series.


Note :To access last 4 rows we can call series_name.tail (4)

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit [Link] for more updates

Selection in Series

Series provides index label loc and ilocand [] to access rows and
columns.

1. loc index label :-

Syntax:-series_name.loc[StartRange: StopRange]

Example-

To Print Values from Index 0 to 2

To Print Values from Index 3 to 4

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit [Link] for more updates

2. Selection Using iloc index label :-

Syntax:-series_name.iloc[StartRange : StopRange]

Example-

To Print Values from Index 0 to 1.

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit [Link] for more updates

3. Selection Using [] :

Syntax:-series_name[StartRange> : StopRange] or

series_name[ index]

Example-

To Print Values at Index 3.

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit [Link] for more updates

Indexing in Series

Pandas provide index attribute to get or set the index of entries or


values in series.

Example-

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit [Link] for more updates

Slicing in Series

Slicing is a way to retrieve subsets of data from a pandas object. A


slice object syntax is –

SERIES_NAME [start:end: step]

The segments start representing the first item, end representing the
last item, and step representing the increment between each item that
you would like.

Example :-

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit [Link] for more updates
DATAFRAME

DATAFRAME-It is a two-dimensional
DATAFEAME object that is useful in
representing data in the form of rows and columns. It is similar to a
spreadsheet or an SQL table. This is the most commonly used pandas
object. Once we store the data into the Dataframe, we can perform
various operations that are useful in analyzing and understanding the
data.

DATAFRAME STRUCTURE

COLUMNS PLAYERNAME IPLTEAM BASEPRICEINCR

0 ROHIT MI 13

1 VIRAT RCB 17

2 HARDIK MI 14

INDEX DATA

PROPERTIES OF DATAFRAME
DATAFEAME
1. A Dataframe has axes (indices)-
➢ Row index (axis=0)
➢ Column index (axes=1)
2. It is similar to a spreadsheet , whose row index is called index and
column index is called column name.
3. A Dataframe contains Heterogeneous data.
4. A Dataframe Size is Mutable.
5. A Dataframe Data is Mutable.

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit [Link] for more updates
A data frame can be created using any of the following-

1. Series
2. Lists
3. Dictionary
4. A numpy 2D array

How to create Dataframe From Series


DATAFEAME
Program-
Output-
import pandas as pd
0
s = [Link](['a','b','c','d']) 0 a
1 b Default Column Name As 0
df=[Link](s)
2 c
print(df) 3 d

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit [Link] for more updates

DataFrame from Dictionary of Series

Example-

DataFrame from List of Dictionaries

Example-

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit [Link] for more updates

Iteration on Rows and Columns

If we want to access record or data from a data frame row wise or


column wise then iteration is used. Pandas provide 2 functions to
perform iterations-

1. iterrows ()
2. iteritems ()

iterrows()

It is used to access the data row wise. Example-

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit [Link] for more updates

iteritems()

It is used to access the data column wise.

Example-

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit [Link] for more updates

Select operation in data frame

To access the column data ,we can mention the column name as
subscript.
e.g. - df[empid] This can also be done by using [Link].
To access multiple columns we can write as df[ [col1, col2,---] ]

Example -

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit [Link] for more updates

>>[Link] or df[‘empid’]
0 101
1 102
2 103
3 104
4 105
5 106
Name: empid, dtype: int64

>>df[[‘empid’,’ename’]]
empid ename
0 101 Sachin
1 102 Vinod
2 103 Lakhbir
3 104 Anil
4 105 Devinder
5 106 UmaSelvi

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit [Link] for more updates

To Add & Rename a column in data


frame

import pandas as pd

s = [Link]([10,15,18,22])

df=[Link](s)

[Link]=[‘List1’] To Rename the default column of Data


Frame as List1

df[‘List2’]=20 To create a new column List2 with all values


as 20

df[‘List3’]=df[‘List1’]+df[‘List2’] Output-

Add Column1 and Column2 and store in List1 List2 List3


0 10 20 30
New column List3 1 15 20 35
2 18 20 38
print(df) 3 22 20 42

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit [Link] for more updates

To Delete a Column in data frame

We can delete the column from a data frame by using any of


the the following –
1. del
2. pop()
3. drop()

>>del df[‘List3’] We can simply delete a column by passing


column name in subscript with df
>>df
Output-

List1 List2
0 10 20
1 15 20
2 18 20
3 22 20

>>[Link](‘List2’) we can simply delete a column by passing column


name in pop method.
>>df

List1
0 10
1 15
2 18
3 22

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit [Link] for more updates

To Delete a Column Using drop()

import pandas as pd
s= [Link]([10,20,30,40])
df=[Link](s)
[Link]=[‘List1’]
df[‘List2’]=40
df1=[Link](‘List2’,axis=1) (axis=1) means to delete Data
column wise
df2=[Link](index=[2,3],axis=0) (axis=0) means to delete
data row wise with given index
print(df)
print(“ After deletion::”)
print(df1)
print (“ After row deletion::”)
print(df2)

Output-
List1 List2
0 10 40
1 20 40
2 30 40
3 40 40
After deletion::
List1
0 10
1 20
2 30
3 40
After row deletion::
List1
0 10
1 20
CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit [Link] for more updates

Accessing the data frame through loc()


and iloc() method or indexing using Labels

Pandas provide loc() and iloc() methods to access the subset from a
data frame using row/column.

Accessing the data frame through loc()

It is used to access a group of rows and columns.

Syntax-

[Link][StartRow : EndRow, StartColumn : EndColumn]


Note -If we pass : in row or column part then pandas provide the entire
rows or columns respectively.

To access a single row

To access multiple Rows Qtr1 to Qtr3

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit [Link] for more updates
Example 2:-

To access single column


TCS

To access Multiple Column namely TCS and WIPRO

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit [Link] for more updates

Example-3

To access first row

To access first 3 Rows

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit [Link] for more updates

Accessing the data frame through iloc()

It is used to access a group of rows and columns based on numeric


index value.

Syntax-

[Link][StartRowindexs : EndRowindex, StartColumnindex : EndColumnindex]

Note -If we pass : in row or column part then pandas provide


the entire rows or columns respectively.

To access First two Rows


and Second column

To access all Rows and First


Two columns Record

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
head() andVisittail() Method
[Link] for more updates

The method head() gives the first 5 rows and the method
tail() returns the last 5 rows.
import pandas as pd
empdata={ 'Doj':['12-01-2012','15-01-2012','05-09-2007',
'17-01-2012','05-09-2007','16-01-2012'],
'empid':[101,102,103,104,105,106],
'ename':['Sachin','Vinod','Lakhbir','Anil','Devinder','UmaSelvi']
}
df=[Link](empdata)
print(df)
print([Link]())
print([Link]())
Output-
Doj empid ename
0 12-01-2012 101 Sachin
1 15-01-2012 102 Vinod
2 05-09-2007 103 Lakhbir Data Frame
3 17-01-2012 104 Anil
4 05-09-2007 105 Devinder
5 16-01-2012 106 UmaSelvi
Doj empid ename
0 12-01-2012 101 Sachin
1 15-01-2012 102 Vinod head() displays first 5 rows
2 05-09-2007 103 Lakhbir
3 17-01-2012 104 Anil
4 05-09-2007 105 Devinder
Doj empid ename
1 15-01-2012 102 Vinod
2 05-09-2007 103 Lakhbir
3 17-01-2012 104 Anil tail() display last 5 rows
4 05-09-2007 105 Devinder
5 16-01-2012 106 UmaSelvi
CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit [Link] for more updates
To display first 2 rows we can use head(2) and to returns last2
rows we can use tail(2) and to return 3rd to 4th row we can write
df[2:5].
import pandas as pd
empdata={ 'Doj':['12-01-2012','15-01-2012','05-09-2007',
'17-01-2012','05-09-2007','16-01-2012'],
'empid':[101,102,103,104,105,106],
'ename':['Sachin','Vinod','Lakhbir','Anil','Devinder','UmaSelvi']
}
df=[Link](empdata)
print(df)
print([Link](2))
print([Link](2))
print(df[2:5])
Output-
Doj empid ename
0 12-01-2012 101 Sachin
1 15-01-2012 102 Vinod
2 05-09-2007 103 Lakhbir
3 17-01- 2012 104 Anil
4 05-09-2007 105 Devinder
5 16-01-2012 106 UmaSelvi

Doj empid ename


0 12-01-2012 101 Sachin head(2) displays first 2 rows
1 15-01-2012 102 Vinod

Doj empid ename


4 05-09-2007 105 Devinder tail(2) displays last 2 rows
5 16-01-2012 106 UmaSelvi
Doj empid ename
2 05-09-2007 103 Lakhbir
3 17-01- 2012 104 Anil df[2:5] display 2nd to 4th row
4 05-09-2007 105 Devinder

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit [Link] for more updates

Boolean Indexing in Data Frame

Boolean indexing helps us to select the data from the DataFrames


using a boolean vector. We create a DataFrame with a boolean index to
use the boolean indexing.

To Return Data frame where index is True

We can pass only integer value in iloc

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit [Link] for more updates

Concat operation in data frame

Pandas provides various facilities for easily combining together Series,


DataFrame.

[Link](objs, axis=0, join='outer', join_axes=None,ignore_index=False)

• objs − This is a sequence or mapping of Series, DataFrame, or


Panel objects.
• axis − {0, 1, ...}, default 0. This is the axis to concatenate along.
• join − {‘inner’, ‘outer’}, default ‘outer’. How to handle indexes on
other axis(es). Outer for union and inner for intersection.
• ignore_index − boolean, default False. If True, do not use the
index values on the concatenation axis. The resulting axis will be
labeled 0, ..., n - 1.
• join_axes − This is the list of Index objects. Specific indexes to
use for the other (n-1) axes instead of performing inner/outer
set logic.

The Concat() performs concatenation operations along an axis.

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit [Link] for more updates

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit [Link] for more updates

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit [Link] for more updates

Merge operation in data frame

Two DataFrames might hold different kinds of information about the


same entity and linked by some common feature/column. To join these
DataFrames, pandas provides multiple functions like merge(), join() etc.

Example-1

This will give the common rows between the


two data frames for the corresponding column
values (‘id’).

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit [Link] for more updates
Example-2

It might happen that the column on which


you want to merge the Data Frames have
different names (unlike in this case). For
such merges, you will have to specify the
arguments left_on as the left DataFrame
name and right_on as the right DataFrame
name.

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit [Link] for more updates

Join operation in data frame

It is used to merge data frames based on some common column/key.

1. Full Outer Join:- The full outer join combines the results of
both the left and the right outer joins. The joined data frame will
contain all records from both the data frames and fill in NaNs for
missing matches on either side. You can perform a full outer join by
specifying the how argument as outer in merge() function.

Example-

The resulting DataFrame had all


the entries from both the tables
with NaN values for missing
matches on either side. However,
one more thing to notice is the
suffix which got appended to the
column names to show which column
came from which DataFrame. The
default suffixes are x and y,
however, you can modify them by
specifying the suffixes argument
in the merge() function.

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit [Link] for more updates
Example-2

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit [Link] for more updates
[Link] Join :- The inner join produce only those records that
match in both the data frame. You have to pass inner in how argument
inside merge() function.

Example-

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit [Link] for more updates

3. RightJoin :-The right join produce a complete set of records


from data frame B(Right side Data Frame) with the matching records
(where available) in data frame A( Left side data frame). If there is no
match right side will contain null. You have to pass right in how
argument inside merge() function.

Example-

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit [Link] for more updates

[Link] Join :- The left join produce a complete set of records


from data frame A(Left side Data Frame) with the matching records
(where available) in data frame B( Right side data frame). If there is
no match left side will contain null. You have to pass left in how
argument inside merge() function.

Example-

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit [Link] for more updates

5. Joining on Index :-Sometimes you have to perform the join on


the indexes or the row labels. For that you have to specify
right_index( for the indexes of the right data frame ) and left_index(
for the indexes of left data frame) as True.

Example-

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit [Link] for more updates

CSV File

A CSV is a comma separated values file, which allows data to


be saved in a tabular format. CSV is a simple file such as a
spreadsheet or database. Files in the csv format can be
imported and exported from programs that store data in
tables, such as Microsoft excel or Open Office.

CSV files data fields are most often


separated, or delimited by a comma. Here the data in each
row are delimited by comma and individual rows are separated
by newline.

To create a csv file, first choose your


favorite text editor such as- Notepad and open a new file.
Then enter the text data you want the file to contain,
separating each value with a comma and each row with a new
line. Save the file with the [Link]. You can open the
file using MS Excel or another spread sheet program. It will
create the table of similar data.

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit [Link] for more updates

pd.read_csv() method is used to read a csv file.

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit [Link] for more updates

Exporting data from dataframe to


CSV File

To export a data frame into a csv file first of all, we create a

data frame say df1 and use dataframe.to_csv(‘

E:\[Link] ’ ) method to export data frame df1 into

csv file [Link].

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit [Link] for more updates

And now the content of df1 is exported to csv file Dataframe1.

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

Unit-2-Data Handling using


Pandas-II

Descriptive Statistics
Statistics is a branch of mathematics that deals with

collecting, interpreting, organization and interpretation of

data. Descriptive statistics involves summarizing and organizing

the data so that it can be easily understood.

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

max()

It returns the maximum value from a column of a data frame or series.

Syntax-

df[‘columnname’].max()

Or

[Link](axis=0) returns the maximum value of every column

Or

[Link](axis=1) returns the maximum value of every row

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

min()

It returns the minimum value from a column of a data frame or series.

Syntax-

df[‘columnname’].min()

Or

[Link] (axis=0) returns the minimum value of every column

Or

[Link](axis=1) returns the minimum value of every row

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

3-count()

It returns the number of values present in a column of a data frame or


series.

Syntax-

df[‘columnname’].count()

Or

[Link](axis=0) returns the number of value in each column

Or

[Link](axis=1) returns the number of value in each row

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

4- mean()

It is used to return the arithmetic mean of a given set of numbers,


mean of a data frame, mean of a column, mean of rows.

Syntax-

df[‘columnname’].mean()

Or

[Link](axis=0) returns the mean of each column

Or

[Link](axis=1) returns the mean of each row

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

5- sum()

It is used to return the addition of all the values of a particular column


of a data frame or a series .

Syntax-

df[‘columnname’].sum()

Or

[Link] (axis=0) returns the sum of each column

Or

[Link] (axis=1) returns the sum of each row

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

6- median()

It is used to return the middle value or median of a given set of numbers,


median of a data frame, median of a column, median of rows.

Syntax-

df[‘columnname’].median()

Or

[Link](axis=0) returns the median of each column

Or

[Link](axis=1) returns the median of each row

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

7- mode()

It is used to return the mode or most repeated value of a given set of


numbers, mode of a data frame, mode of a column, mode of rows.

Syntax-

df[‘columnname’].mode()

Or

[Link](axis=0) returns the mode of each column

Or

[Link](axis=1) returns the mode of each row

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

8- quartile()

The word ‘’quartile” is taken from the word ‘’quantile’’ and the word

‘’quantile’’ taken from the ‘’quantity’’. Let us understand this by taking an

example-

The 0.35 quantile states that 35% of the observations in the

dataset are below a given line. It also states that there are

65% remaining observations are above the line.

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

Method to find Quartiles?

Let us take an example: suppose we have numbers- 1,3,4,7,8,8,9

Step 1: Arrange the data in ascending order (already in


ascending order)

Step 2: Count total number of observation, say n=7

Step 3: Find out first quartile i.e. Q1 (25%) say 0.25


also called 25TH percentile

Step 4: Now calculate Q1=round (.25(n+1))= round


(.25(7+1))
= round (.25(8)) = 2.0 it means 2ND Observation i.e. 3

Step 5: Calculate second quartile i.e. Q2 (50%) = 0.50


or 50TH percentile
= round (.50(7+1)) = 4TH observation i.e. 7

Step 6: Calculate third Quartile i.e. Q3 (75%) =0.75 or


75TH percentile = round (.75(7+1)) = 6TH observation=8

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

Program to Find Quartile-

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

9- Variance

It is used to return the variance of a given set of numbers, a data


frame, column, rows.

Syntax-

df[‘columnname’].var()

Or

[Link](axis=0) returns the variance of each column

Or

[Link](axis=1) returns the variance of each row

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

10- Standard deviation

It is used to return the standard deviation of a given set of numbers, a


data frame, column, rows.

Syntax-

df[‘columnname’].std()

Or

[Link](axis=0) returns the standard deviation of each column

Or

[Link](axis=1) returns the standard deviation of each row

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

Groupby()

A groupby() function involves one of the following operations


on the data frame –

1. Splitting the data frame


2. Applying a function (usually an aggregate function)
3. Combining the result

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

Example:- Program to group the data- city wise and find out
maximum temperature according to the city.

28 :-Temp in morning and

30:-Temp in Evening

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

Sorting

Sorting in data frame can be done row wise or column wise. By default
sorting is done row wise.

Pandas provide two types of sort functions-

1. sort_values(): To sort the data of a given column in ascending or


descending order.
2. sort_index(): To sort the data based on index value.

sort_values() : To sort the data of a given column in


ascending or descending order.

Syntax:-

df.sort_values(by=’col_name’, ascending=True or False, inplace =True or False)

by: Give column name on which you want to perform sorting.

Ascending : By default ascending is true.

Inplace : By default inplace is false. It means if you do not want to create


a new data frame then set its value as True.

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

Example 1- to sort a data frame in ascending order of a column.

For performing sorting in ascending order we do-

df.sort_values ( ‘column name’) or

df.sort_values(by=’column_name’)

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

Example 2- To sort a data frame in descending order of a


column.

For performing sorting in descending order we do-

df.sort_values ( ‘column name’, ascending=False or (0) )

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

Example 3- To sort a data frame based on multiple column.

For performing sorting based on multiple column we do-

df.sort_values (by=[‘col1’, ‘col2’], ascending=[(True or False), (True or False) ]

As we are sorting the data in ascending


order of Percentage so when two values
in Percentage are same then data frame
will be sorted in descending order of Roll
Number.

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

Example 4- If you do not want to modify your data frame


after sorting.

For this we do-

df.sort_values (by= ‘column name’, ascending=False or True,


inplace=True)

By default inplace is False.

If you do not want to create a


new data frame.

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

sort_index()
To sort the data based on index Value.

Syntax:

df.sort_index(by=None, ascending=True or False, inplace =True or False)

by: Give column name on which you want to perform sorting.

Ascending : By default ascending is true.

Inplace : By default inplace is false. It means if you do not want to create


a new data frame then set its value as True.

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

Example 1:- To sort the data frame based on index in ascending order

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

Example 2:- To sort the data frame based on index in descending order

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

Renaming index

rename () method is used to rename the indexes in a data frame.

Syntax- [Link] ( index, inplace (optional))

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

Deleting index

reset_index().drop() method is used to delete the indexes in a data


frame.

Syntax- df. reset_index().drop( index, inplace (optional))

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

PIVOTING AND AGGREGATION

Pivoting- Pivoting is one of the important aspect of data analyst. It is


used to summarize large amount of data and permit us to access
important records from a large dataset.

Python Pandas provide two functions for pivoting.

1. pivot()
2. pivot-table()

pivot()

pivot()- pivot() allows us to transform or reshape the data frame based

on the column values according to our perspective. It takes 3

arguments – (index, columns and values).

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

Dataframe(df)

Pivoting and Aggregation

import pandas as pd
data={
'Year':['2018','2019','2018','2019','2018','2019'],
'Team':['MI','MI','RCB','RCB','CSK','CSK'],

'Runs':[2500,2650,2200,2400,2300,2700]}

df=[Link](data)
pv=[Link](df,index='Year',columns='team',values='Runs')
print (df)
print (pv)
CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

Output-

Year Team Runs

0 2018 MI 2500

1 2019 MI 2650

2 2018 RCB 2200

3 2019 RCB 2400

4 2018 CSK 2300

5 2019 CSK 2700

Team CSK MI RCB

Year

2018 2300 2500 2200

2019 2700 2650 2400

pivot_table()

pivot_table() :- we know that pivot() method takes at least 2 column

names as parameters - the index and the columns named parameters.

What will happen if we have multiple rows with the same values for these

columns.

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

The pivot_table() method comes to solve this problem. It works like

pivot, but it aggregates the values from rows with duplicate entries for

the specified columns (means apply aggregate function specify by us).

By default pivot_table() apply mean() to aggregate the values from rows

with duplicate entries for the specified columns. E.g.

#program to Find City wise temperature


Program-
import pandas as pd
data={
'Date':['1-1-2019','1-1-2019','1-2-2019','1-2-2019','1-3-2019','1-
3-2019'],
CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

'City':['DELHI','DELHI','MUMBAI','MUMBAI','CHENNAI','CH
ENNAI'],

'Temp':[28,30,22,24,32,34],
'Humidity':[60,55,80,70,90,85]
}
df=[Link](data)
print (df)
pv=pd.pivot_table(df,index='City',values='Temp')
print (pv)

Output-

Date Humidity Temp City

0 1-1-2019 60 28 DELHI
1 1-1-2019 55 30 DELHI
2 1-2-2019 80 22 MUMBAI
3 1-2-2019 70 24 MUMBAI
4 1-3-2019 90 32 CHENNAI
5 1-3-2019 85 34 CHENNAI
Temp
City
CHENNAI 33
DELHI 29
MUMBAI 23

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

#Program to find City Wise Maximum temperature

import pandas as pddata={

'Date':['1-1-2019','1-1-2019','1-2-2019','1-2-2019','1-3-2019','1-
3-2019'],

'city':['DELHI','DELHI','MUMBAI','MUMBAI','CHENNAI','CH
ENNAI'],

'Temp':[28,30,22,24,32,34],

'Humidity':[60,55,80,70,90,85]

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

df=[Link](data)

print (df)

pv=pd.pivot_table(df,index='city',values='Temp', aggfunc='max')

print (pv)

Output-

Date Humidity Temp city


0 1-1-2019 60 28 DELHI
1 1-1-2019 55 30 DELHI
2 1-2-2019 80 22 MUMBAI
3 1-2-2019 70 24 MUMBAI
4 1-3-2019 90 32 CHENNAI
5 1-3-2019 85 34 CHENNAI
Temp
city
DELHI 30
MUMBAI 24
CHENNAI 34

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

#Program to print data frame on date index and city column


Example 3-
import pandas as pd
data={
'Date':['1-1-2019','1-1-2019','1-2-2019','1-2-2019','1-3-
2019','1-3-2019'],

'city':['DELHI','DELHI','MUMBAI','MUMBAI','CHENNAI
','CHENNAI'],
'Temp':[28,30,22,24,32,34],

'Humidity':[60,55,80,70,90,85]

df=[Link](data)

print (df)

print(pd.pivot_table(df,index='Date',columns='city'))

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

Output-

Date Humidity Temp city


0 1-1-2019 60 28 DELHI
1 1-1-2019 55 30 DELHI
2 1-2-2019 80 22 MUMBAI
3 1-2-2019 70 24 MUMBAI
4 1-3-2019 90 32 CHENNAI
5 1-3-2019 85 34 CHENNAI
Humidity Temp
city CHENNAI DELHI MUMBAI CHENNAI DELHI MUMBAI
Date Not a Number or a Missing Value

1-1-2019 NaN 57.5 NaN NaN 29.0 NaN


1-2-2019 NaN NaN 75.0 NaN NaN 23.0
1-3-2019 87.5 NaN NaN 33.0 NaN NaN

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

Handling Missing Values- filling & Dropping

In many cases, the data that we receive from many sources may not be
perfect. That means there may be some missing data. For example- in
the given program where employee name is missing in one row and date
of joining is missing in other row.

When we convert the data into data frame, the missing data is
represented by NaN (Not a Number). NaN is a default marker for
the missing value.

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

Consider the following Data Frame-

We can use fillna() method to replace NaN or Na value by a


specified value.

For example- to fill the Nan value by 0.

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

But this is not useful as it is filling any type of column with 0. We can
fill each column with a different value by passing the column name and
the value to be used to fill in that column.

For example- to fill ‘ename’ with ‘Name Missing’ and ‘Doj’ wityh ’00-00-

0000’. We should supply these values as a dictionary inside fillna()

method.

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

If we do not want any missing data and want to remove those rows
having Na or NaN values, then we can use dropna() method.

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

Importing-Exporting Data between


MySql and Python Pandas
For importing and exporting data between Mysql and Python Pandas we

need to install mysql connector and mysql client module.

Installing and importing mysql connector, mysql


client-
With Anaconda : if we have installed python using Anaconda, then
mysql connector and mysql client need to be installed on your computer.

We can check this in Anaconda Navigator, by Clicking on not installed in

Environment and then scroll down to find mysql connector and mysql

client and by clicking on both these, install them in Anaconda.

Steps to import and export data using pandas and Mysql

1. Start Python
2. import [Link] package
3. Create or open a database
4. Open and establish a connection to the database
5. Create a cursor object or its instance (required for Pandas to
Mysql)
6. Read a sql query for (Mysql to Pandas) and execute a query for(
Pandas to Mysql)
7. Commit the transaction for(Pandas to Mysql)

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

8. Close the connection for(Pandas to Mysql)

Exporting Data between Python Pandas & Mysql

Program 1- To insert and Delete record in MySql from Pandas data


frame.

Before execution of the program employee table contains no record.

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

For extracting data from data frame into


different columns

For casting integer to string

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

After the execution of the program the records in employee table are-

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

Example 2-

To perform Update operation in MySql from Pandas data frame.

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

After the execution of the program the record in employee table got
updated from Sachin to Sachin Bhardwaj-

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

Importing Data between Python Pandas & Mysql

Example 1- To retrieve column empid and Doj from employee table into
data frame emp.

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

Example -2

To retrieve all the tables from database sachin into data frame emp.

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

Importing-Exporting Data between


MySql and Python Pandas USING
Sqlalchemy
Sqlalchemy is a database manipulation tool for python which can be used

as standalone library to manipulate relational databases. Sqlalchemy

provide core python based sql expressions and object oriented python

based ORM (Object Relational Mapper). it also provide high level

declarative syntax for ORM for simplicity.

Sqlalchemy follow data mapper pattern and inspired from java

hibernate. To work with sqlalchemy first of all we need to install

following library:

1. Slalchemy ( -m pip install sqlalchemy)

2. PyMySQL (-m pip install PyMySQL)

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

Importing Data between Python Pandas & MySQL


using sqlalchemy

In Above program-

User Name of MYSQL is- root

Password of MYSQL is- 123

Database in MYSQL is- sachin

Table from which records are fetched is- record

that is already created in MYSQL with 3 records.

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

Importing Data between Python Pandas &


MySQL using sqlalchemy based on specific
Columns

Importing Data between Python Pandas &


MySQL using sqlalchemy based on specific
Condition

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

Exporting Data between Python Pandas &


Mysql using sqlalchemy

The to_sql() function is


used to write the records
stored in a DataFrame to
a SQL Table.

After the execution of the above program

MYSQL database sachin looks like:

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

Example-2

After the execution of the above program

MYSQL ipl tables looks like:

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR
For More Updates Visit: [Link]

CREATED BY: SACHIN BHARDWAJ, PGT (CS) KV NO.1 TEZPUR, MR. VINOD KUMAR VERMA,
PGT (CS) KV OEF KANPUR

You might also like