Traversing Dataframe Elements Using: Iterrows, Iteritems and Itertuples
Traversing Dataframe Elements Using: Iterrows, Iteritems and Itertuples
To iterate over the rows of the DataFrame, we can use the following functions −
Name Age
0 Shalini 23
1 Varsha 56
2 Shanti 54
3 Madhu 34
ITER ROWS
Hello Shalini
Hello 23
Hello Varsha
Hello 56
Hello Shanti
Hello 54
Hello Madhu
Hello 34
5.2 USE of iteritems()
>>>print('ITER ITEMS')
ITER ITEMS
Hello Shalini
Hello Varsha
Hello Shanti
Hello Madhu
Hello 23
Hello 56
Hello 54
Hello 34
www.pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan, PGT CS , KV NO.3 Gwalior
5.3 USE of itertuples()
>>>print('ITER TUPLES')
print(rows)
ITER TUPLES
Pandas(Index=0, Name='Shalini', Age=23)
Pandas(Index=1, Name='Varsha', Age=56)
Pandas(Index=2, Name='Shanti', Age=54)
Pandas(Index=3, Name='Madhu', Age=34)
>>> s2=[[3,2,5],[5,7,8]]
>>> s3=[[5,5,5],[4,4,4]]
>>> dfr1=pd.DataFrame(s1)
Created three data frames namely
>>> dfr2=pd.DataFrame(s2) dfr1, dfr2 and dfr3
>>> dfr3=pd.DataFrame(s3)
6.1 ADDITION
>>> dfr1
0 1 2
0 1 2 3
1 4 5 6
>>> dfr2
0 1 2
0 3 2 5
1 5 7 8
>>> dfr3
0 1 2
0 5 5 5
1 4 4 4
>>> dfr1.add(dfr2)
0 1 2 It will add Corresponding elements of dfr2 with dfr1
0 4 4 8 (dfr2+dfr1)
1 9 12 14
>>> dfr1.radd(dfr2)
0 1 2 Here ‘r’ stands for reverse it will add Corresponding elements
0 4 4 8 of dfr2 with dfr1 (dfr2+dfr1)
1 9 12 14
>>> dfr3+dfr1+dfr2
0 1 2 It will add Corresponding elements of dfr1, dfr2 and dfr13
0 9 9 13
1 13 16 18
6.2
SUBTRACTION
>>> dfr1-dfr2
0 1 2 It will subtract Corresponding elements of dfr1 with dfr2
0 -2 0 -2
1 -1 -2 -2
>>> dfr1.sub(dfr2)
0 1 2 It will subtract Corresponding elements of dfr1 with dfr2
0 -2 0 -2
1 -1 -2 -2
>>> dfr1.rsub(dfr2)
Here ‘r’ stands for reverse it will subtract Corresponding
0 1 2
0 2 0 2
elements of dfr2 with dfr1 (dfr2 - dfr1)
1 1 2 2
>>> dfr1-2
0 1 2
Here 2 is subtracted with each element of Data Frame dfr1
0 -1 0 1
1 2 3 4
7.2 Broadcasting : Enlarging the smaller object in a binary operation by replicating its
elements so as to match the shape of larger object.
1
www.pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan, PGT CS , KV NO.3 Gwalior
8. Handling Missing Data :
As data comes in many shapes and forms, pandas aims to be flexible with regard to handling
missing data. While NaN is the default missing value marker for reasons of computational speed
and convenience, we need to be able to easily detect this value with data of different types:
floating point, integer, boolean, and general object. In many cases, however, the
Python None will arise and we wish to also consider that “missing” or “not available” or “NA”.
Dropna(how=’all’) It will remove nly those rows that have all NaN values
fillna(<dictionary It will fill missing Values with the value specified
Values>)
>>>import pandas as pd
>>>KV_shift1={'Computer':[20,25,22,50],'Projectors':[1,1,1,14],'iPad':[1,1,1,7],'AppleTv':[1,1,1,7]}
>>>dfr1=pd.DataFrame(KV_shift1,index=['SrCompLab','SecCompLab','PriLab','Others'])
>>>print(dfr1)
>>>KV_shift2={'Computer':[20,25,22,50],'Visualizers':[1,1,1,14],'iPad':[1,1,1,7],'AppleTv':[1,1,1,7]}
>>>dfr2=pd.DataFrame(KV_shift2,index=['SrCompLab','SecCompLab','PriLab','Others'])
>>>print(dfr2)
>>>KV3Gwl=dfr1+dfr2
>>>print(KV3Gwl)
NOTNULL ()
AppleTv Computer Projectors Visualizers iPad
SrCompLab True True False False True
SecCompLab True True False False True
PriLab True True False False True
Others True True False False True
>>> KV3Gwl
AppleTv Computer Projectors Visualizers iPad
SrCompLab 2 40 6.0 2.0 2
SecCompLab 2 50 NaN 5.0 2
PriLab 2 44 7.0 NaN 2
Others 14 100 5.0 2.0 14
>>> KV3Gwl.dropna()
AppleTv Computer Projectors Visualizers iPad
SrCompLab 2 40 6.0 2.0 2
Others 14 100 5.0 2.0 14
import pandas as pd
import numpy as np
Data Frame 1 :
Computer Projectors iPad AppleTv
SrCompLab 20 1.0 NaN 1
SecCompLab 25 1.0 1.0 1
PriLab 22 NaN 2.0 1
Others 50 14.0 7.0 7
Data Frame 2 :
Computer Projectors iPad AppleTv
SrCompLab 20 1.0 NaN 1.0
SecCompLab 25 1.0 1.0 1.0
PriLab 22 NaN 2.0 1.0
Others 50 14.0 7.0 7.0
if df1.empty==True:
print('Data1 Frame is Empty')
if df2.empty==True:
print('Data Frame2 is Empty')
else:
print('Data Frame2 is not Empty')
print('Data Frame')
print(df2)
print('Used function all()')
print((df2<5).all())
print('Used function any()')
print((df2<5).any())
Data1 Frame is Empty
Data Frame2 is not Empty
Data Frame
0 1 2
0 2 5 8
1 10 5 2