Python Pandas Module - Introduction-07-11-2023
Python Pandas Module - Introduction-07-11-2023
2
Python Pandas
Pandas is an open-source Python Library providing high-
performance data manipulation and analysis tool using its powerful
data structures.
The name Pandas is derived from the word Panel Data – an
Econometrics from Multidimensional data.
3
Key Features of Pandas
Fast and efficient DataFrame object with default and customized
indexing.
Tools for loading data into in-memory data objects from different
file formats.
Data alignment and integrated handling of missing data.
Reshaping and pivoting of date sets.
Label-based slicing, indexing and subsetting of large data sets.
4
Key Features of Pandas
Columns from a data structure can be deleted or inserted.
Group by data for aggregation and transformations.
High performance merging and joining of data.
Time Series functionality.
5
Pandas deals with the following three data structures
Series
DataFrame
Panel
These data structures are built on top of Numpy array, which means they
are fast.
6
Dimension & Description
The best way to think of these data structures is that the higher
dimensional data structure is a container of its lower dimensional
data structure.
For example, DataFrame is a container of Series, Panel is a container
of DataFrame.
7
Dimension & Description
Data
Dimensions Description
Structure
1D labeled homogeneous array,
Series 1
sizeimmutable.
General 2D labeled, size-mutable tabular
Data
2 structure with potentially heterogeneously
Frames
typed columns.
Panel 3 General 3D labeled, size-mutable array.
8
Dimension & Description
9
Mutability
All Pandas data structures are value mutable (can be changed) and
except Series all are size mutable. Series is size immutable.
Note − DataFrame is widely used and one of the most important data
structures. Panel is used much less.
10
Pandas Series
11
Pandas DataFrame
DataFrame is a two-dimensional array with heterogeneous data. For example,
12
Pandas Panel
Panel is a three-dimensional data structure with heterogeneous
data.
It is hard to represent the panel in graphical representation.
But a panel can be illustrated as a container of DataFrame.
13
Python Pandas - Series
Series is a one-dimensional labeled array capable of holding data of any
type (integer, string, float, python objects, etc.). The axis labels are
collectively called index.
pandas.Series
A pandas Series can be created using the following constructor −
14
Python Pandas - Series
The parameters of the constructor are as follows −
Sr.
Parameter & Description
No
1 data data takes various forms like ndarray, list, constants
index Index values must be unique and hashable, same length as
2
data. Default np.arrange(n) if no index is passed.
3 dtype dtype is for data type. If None, data type will be inferred
4 copy Copy data. Default False
15
Python Pandas - Series
A series can be created using various inputs like −
Array
Dict
Scalar value or constant
16
Create an Empty Series
A basic series, which can be created is an Empty Series.
Example
#import the pandas library and aliasing as pd
import pandas as pd
s = pd.Series()
print(s)
17
Python Pandas - DataFrame
A Data frame is a two-dimensional data structure, i.e., data is aligned in a
tabular fashion in rows and columns.
Features of DataFrame
Potentially columns are of different types
Size – Mutable
Labelled axes (rows and columns)
Can Perform Arithmetic operations on rows and columns
18
Structure
Let us assume that we
are creating a data
frame with student’s
data.
You can think of it as
an SQL table or a
spreadsheet data
representation.
19
pandas.DataFrame
A pandas DataFrame can be created using the following constructor −
20
pandas.DataFrame
The parameters of the constructor are as follows −
data data takes various forms like ndarray, series, map, lists, dict, constants and also
1
another DataFrame.
index For the row labels, the Index to be used for the resulting frame is Optional Default
2
np.arange(n) if no index is passed.
columns For column labels, the optional default syntax is - np.arange(n). This is only true if
3
no index is passed.
5 copy This command (or whatever it is) is used for copying of data, if the default is False.
21
Create DataFrame
A pandas DataFrame can be created using various inputs like −
Lists
dict
Series
Numpy ndarrays
Another DataFrame
In the subsequent sections of this chapter, we will see how to create a
DataFrame using these inputs.
22
Create an Empty DataFrame
A basic DataFrame, which can be created is an Empty Dataframe.
Example
#import the pandas library and aliasing as pd
import pandas as pd
df = pd.DataFrame()
print(df)
Empty DataFrame
Columns: []
Index: []
23
Create a DataFrame from Lists
The DataFrame can be created using a single list or a list of lists.
Example 1
import pandas as pd
data = [1,2,3,4,5]
df = pd.DataFrame(data)
print(df)
0
0 1
1 2
2 3
3 4
4 5
24
Example 2
import pandas as pd
data = [['Alex',10],['Bob',12],['Clarke',13]]
df = pd.DataFrame(data,columns=['Name','Age'])
print(df)
Name Age
0 Alex 10
1 Bob 12
2 Clarke 13
25
Example 3
import pandas as pd
data = [['Alex',10],['Bob',12],['Clarke',13]]
df = pd.DataFrame(data,columns=['Name','Age'],dtype=float)
print(df)
Name Age
0 Alex 10.0
1 Bob 12.0
2 Clarke 13.0
Note − Observe, the dtype parameter changes the type of Age column to
floating point.
26
Create a DataFrame from Dict of ndarrays / Lists
All the ndarrays must be of same length. If index is passed, then the
length of the index should equal to the length of the arrays.
If no index is passed, then by default, index will be range(n), where n is
the array length.
27
Example 1
import pandas as pd
data = {'Name':['Tom', 'Jack', 'Steve', 'Ricky'],'Age':[28,34,29,42]}
df = pd.DataFrame(data)
print(df)
Age Name
0 28 Tom
1 34 Jack
2 29 Steve
3 42 Ricky
Note − Observe the values 0,1,2,3. They are the default index assigned to
each using the function range(n).
28
Example 2
Let us now create an indexed DataFrame using arrays.
import pandas as pd
data = {'Name':['Tom', 'Jack', 'Steve', 'Ricky'],'Age':[28,34,29,42]}
df = pd.DataFrame(data, index=['rank1','rank2','rank3','rank4'])
print(df)
Age Name
rank1 28 Tom
rank2 34 Jack
rank3 29 Steve
rank4 42 Ricky
30
Example 1
The following example shows how to create a DataFrame by passing a list of
dictionaries.
import pandas as pd
data = [{'a': 1, 'b': 2},{'a': 5, 'b': 10, 'c': 20}]
df = pd.DataFrame(data)
print(df)
a b c
0 1 2 NaN
1 5 10 20.0
import pandas as pd
data = [{'a': 1, 'b': 2},{'a': 5, 'b': 10, 'c': 20}]
df = pd.DataFrame(data, index=['first', 'second'])
print(df)
a b c
first 1 2 NaN
second 5 10 20.0
32
Example 3
The following example shows how to create a DataFrame with a list of
dictionaries, row indices, and column indices.
import pandas as pd
data = [{'a': 1, 'b': 2},{'a': 5, 'b': 10, 'c': 20}]
#With two column indices with one index with other name
df2 = pd.DataFrame(data, index=['first', 'second'], columns=['a', 'b1'])
print(df1)
print(df2)
33
Its output is as follows −
#df1 output
a b
first 1 2
second 5 10
#df2 output
a b1
first 1 NaN
second 5 NaN
Example
import pandas as pd
df = pd.DataFrame(d)
print(df)
35
Its output is as follows −
one two
a 1.0 1
b 2.0 2
c 3.0 3
d NaN 4
Note − Observe, for the series one, there is no label ‘d’ passed, but in
the result, for the d label, NaN is appended with NaN.
Let us now understand column selection, addition, and deletion
through examples.
36
Column Selection
We will understand this by selecting a column from the DataFrame.
Example
import pandas as pd
df = pd.DataFrame(d)
print(df['one'])
37
Its output is as follows −
a 1.0
b 2.0
c 3.0
d NaN
Name: one, dtype: float64
38
Column Addition
We will understand this by adding a new column to an existing data frame.
Example
import pandas as pd
df = pd.DataFrame(d)
# Adding a new column to an existing DataFrame object with column label by passing new series
print(df)
39
Its output is as follows −
40
Row Selection, Addition, and Deletion
We will now understand row selection, addition and deletion through
examples. Let us begin with the concept of selection.
Selection by Label
Rows can be selected by passing row label to a loc function.
import pandas as pd
df = pd.DataFrame(d)
print(df.loc['b'])
41
Its output is as follows −
one 2.0
two 2.0
Name: b, dtype: float64
42
Selection by integer location
Rows can be selected by passing integer location to an iloc function.
import pandas as pd
df = pd.DataFrame(d)
print(df.iloc[2])
one 3.0
two 3.0
Name: c, dtype: float64
43
Slice Rows
Multiple rows can be selected using ‘ : ’ operator.
import pandas as pd
df = pd.DataFrame(d)
print(df[2:4])
one two
c 3.0 3
d NaN 4
44
Addition of Rows
Add new rows to a DataFrame using the append function. This function will append the
rows at the end.
import pandas as pd
df = df.append(df2)
print(df)
a b
0 1 2
1 3 4
0 5 6
1 7 8
45
Deletion of Rows
Use index label to delete or drop rows from a DataFrame. If label is duplicated,
then multiple rows will be dropped.
If you observe, in the above example, the labels are duplicate. Let us drop a
label and will see how many rows will get dropped.
import pandas as pd
df = df.append(df2)
print(df)
46
Its output is as follows −
a b
1 3 4
1 7 8
In the above example, two rows were dropped because those two
contain the same label 0.
47
Deletion of Columns
Use index label to delete or drop columns from a DataFrame using
pd.drop(column_name, axis = 1) . If label is duplicated, then multiple
columns will be dropped.
import pandas as pd
df = df.append(df2)
print(df)
48
Its output is as follows −
a
0 1
1 3
0 5
1 7
49
Python Pandas - Function Application
To apply your own or another library’s functions to Pandas objects, you
should be aware of the three important methods. The methods have
been discussed below. The appropriate method to use depends on
whether your function expects to operate on an entire DataFrame, row-
or column-wise, or element wise.
Table wise Function Application: pipe()
Row or Column Wise Function Application: apply()
Element wise Function Application: applymap()
50
Table-wise Function Application
Custom operations can be performed by passing the function and the
appropriate number of parameters as pipe arguments. Thus, operation is
performed on the whole DataFrame.
For example, add a value 2 to all the elements in the DataFrame. Then,
adder function
The adder function adds two numeric values as parameters and returns the
sum.
def adder(ele1,ele2):
return ele1+ele2
51
We will now use the custom function to conduct operation on the
DataFrame.
df = pd.DataFrame(np.random.randn(5,3),columns=['col1','col2','col3'])
df.pipe(adder,2)
import pandas as pd
import numpy as np
def adder(ele1,ele2):
return ele1+ele2
df = pd.DataFrame(np.random.randn(5,3),columns=['col1','col2','col3'])
print(df.pipe(adder,2))
52
Its output is as follows −
53
Row or Column Wise Function Application
Arbitrary functions can be applied along the axes of a DataFrame or
Panel using the apply() method, which, like the descriptive statistics
methods, takes an optional axis argument. By default, the operation
performs column wise, taking each column as an array-like.
Example 1
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(5,3),columns=['col1','col2','col3'])
print(df.apply(np.mean))
54
Its output is as follows −
col1 0.778735
col2 -0.507344
col3 -0.963479
dtype: float64
55
Example 2
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(5,3),columns=['col1','col2','col3'])
print(df.apply(np.mean,axis=1))
0 0.308779
1 -0.059966
2 1.090742
3 0.563616
4 0.616710
dtype: float64
56
Example 3
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(5,3),columns=['col1','col2','col3'])
df1 = df.apply(lambda x: x.max() - x.min())
print(df1)
col1 2.899872
col2 2.941797
col3 2.500316
dtype: float64
57
Example 4
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(5,3),columns=['col1','col2','col3'])
df1 = df.apply(lambda x: x.max() - x.min(), axis=1)
print(df1)
0 1.435421
1 1.685351
2 0.552591
3 0.338889
4 2.111921
dtype: float64
58
Element Wise Function Application
Not all functions can be vectorized (neither the NumPy arrays which return
another array nor any value), the methods applymap() on DataFrame (and
analogously map() on Series) accepts any Python function taking a single
value and returning a single value.
Example 1
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(5,3),columns=['col1','col2','col3'])
# My custom function
df1 = df['col1'].map(lambda x:x*100)
print(df1)
59
Its output is as follows −
0 -28.092383
1 15.497209
2 48.183373
3 -11.853294
4 -71.943119
Name: col1, dtype: float64
60
Example 2
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(5,3),columns=['col1','col2','col3'])
df1 = df.applymap(lambda x:x*100)
print(df1)
62
Example
import pandas as pd
import numpy as np
N=20
df = pd.DataFrame({
'A': pd.date_range(start='2016-01-01',periods=N,freq='D'),
'x': np.linspace(0,stop=N-1,num=N),
'y': np.random.rand(N),
'C': np.random.choice(['Low','Medium','High'],N).tolist(),
'D': np.random.normal(100, 10, size=(N)).tolist()
})
print(df_reindexed)
63
Its output is as follows −
A C B
0 2016-01-01 Low NaN
2 2016-01-03 High NaN
5 2016-01-06 Low NaN
64
Reindex to Align with Other Objects
You may wish to take an object and reindex its axes to be labeled the
same as another object. Consider the following example to understand
the same.
Example
import pandas as pd
import numpy as np
df1 = pd.DataFrame(np.random.randn(10,3),columns=['col1','col2','col3'])
df2 = pd.DataFrame(np.random.randn(7,3),columns=['col1','col2','col3'])
df1 = df1.reindex_like(df2)
print(df1)
65
Its output is as follows −
Note − Here, the df1 DataFrame is altered and reindexed like df2. The
column names should be matched or else NAN will be added for the
entire column label.
66
Filling while ReIndexing
reindex() takes an optional parameter method which is a filling method
with values as follows −
pad/ffill − Fill values forward
bfill/backfill − Fill values backward
nearest − Fill from the nearest index values
67
Example
import pandas as pd
import numpy as np
df1 = pd.DataFrame(np.random.randn(6,3),columns=['col1','col2','col3'])
df2 = pd.DataFrame(np.random.randn(2,3),columns=['col1','col2','col3'])
# Padding NAN's
print(df2.reindex_like(df1))
68
Its output is as follows −
Example
import pandas as pd
import numpy as np
df1 = pd.DataFrame(np.random.randn(6,3),columns=['col1','col2','col3'])
df2 = pd.DataFrame(np.random.randn(2,3),columns=['col1','col2','col3'])
# Padding NAN's
print(df2.reindex_like(df1))
Note − Observe, only the 7th row is filled by the preceding 6th row. Then, the rows are
left as they are.
71
Renaming
The rename() method allows you to relabel an axis based on some
mapping (a dict or Series) or an arbitrary function.
Let us consider the following example to understand this −
import pandas as pd
import numpy as np
df1 = pd.DataFrame(np.random.randn(6,3),columns=['col1','col2','col3'])
print df1
import pandas as pd
import numpy as np
N=20
df = pd.DataFrame({
'A': pd.date_range(start='2016-01-01',periods=N,freq='D'),
'x': np.linspace(0,stop=N-1,num=N),
'y': np.random.rand(N),
'C': np.random.choice(['Low','Medium','High'],N).tolist(),
'D': np.random.normal(100, 10, size=(N)).tolist()
})
A
C
D
x
y
To iterate over the rows of the DataFrame, we can use the following
functions −
iteritems() − to iterate over the (key,value) pairs
iterrows() − iterate over the rows as (index,series) pairs
itertuples() − iterate over the rows as namedtuples
76
iteritems()
Iterates over each column as key, value pair with label as key and
column value as a Series object.
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(4,3),columns=['col1','col2','col3'])
for key,value in df.iteritems():
print(key,value)
77
Its output is as follows −
col1 0 0.802390
1 0.324060
2 0.256811
3 0.839186
Name: col1, dtype: float64
col2 0 1.624313
1 -1.033582
2 1.796663
3 1.856277
Name: col2, dtype: float64
col3 0 -0.022142
1 -0.230820
2 1.160691
3 -0.830279
Name: col3, dtype: float64
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(4,3),columns = ['col1','col2','col3'])
for row_index,row in df.iterrows():
print(row_index,row)
79
Its output is as follows −
0 col1 1.529759
col2 0.762811
col3 -0.634691
Name: 0, dtype: float64
1 col1 -0.944087
col2 1.420919
col3 -0.507895
Name: 1, dtype: float64
2 col1 -0.077287
col2 -0.858556
col3 -0.663385
Name: 2, dtype: float64
3 col1 -1.638578
col2 0.059866
col3 0.493482
Name: 3, dtype: float64
Note − Because iterrows() iterate over the rows, it doesn't preserve the data type across
the row. 0,1,2 are the row indices and col1,col2,col3 are column indices. 80
itertuples()
itertuples() method will return an iterator yielding a named tuple for
each row in the DataFrame. The first element of the tuple will be the
row’s corresponding index value, while the remaining values are the row
values.
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(4,3),columns = ['col1','col2','col3'])
for row in df.itertuples():
print(row)
81
Its output is as follows −
Pandas(Index=0, col1=1.5297586201375899, col2=0.76281127433814944, col3=-
0.6346908238310438)
82
Note − Do not try to modify any object while iterating. Iterating is
meant for reading and the iterator returns a copy of the original object
(a view), thus the changes will not reflect on the original object.
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(4,3),columns = ['col1','col2','col3'])
83
Its output is as follows −
84