You are on page 1of 15

DATAFRAME

PART II
Displaying a DataFrame

In[14]:print(dtf4)
Out[14]:
Population Avg.Income PerCapitaIncome
D 1.45655 102983 150000
E 1.18899 134567 160000
K 2.57575 132000 340000
C 1.30434 345002 450000
Iterating over a DataFrame

Syntax:
<DFobject>.iterrows( )
OR
<DFobject>.iteritems( )
Iterating over a DataFrame

(i)<DF>.iterrows-It views a dataframe in the form


of horizontal subsets i.e.,row-wise.Each horizontal
subset in the form of (row-index,Series) where
Series contains all column values for that
row -index.
(ii)<DF>.iteritems-It views a dataframe in the form
of vertical subsets i.e.,column-wise.Each vertical
subset in the form of (col-index,Series) where
Series contains all row values for that
column-index.
Iterating over a DataFrame

In[1]: disales={2015 : {'qt1' : 30000,'qt2‘:50000,


'qt3':45000, 'qt4':60000}, 2016:{'qt1':
40000,'qt2':50000,'qt3':49000,'qt4':20000},
2017: {'qt1' :60000,'qt2‘:33000,
'qt3':55000, 'qt4':90000}}
In[2]:df1=pd.DataFrame(disales)
In[3]:df1
Iterating over a DataFrame

Out[3]:
2015 2016 2017
qt1 30000 40000 60000
qt 2 50000 50000 33000
qt3 45000 49000 55000
qt4 60000 20000 90000
Iterating over a DataFrame

<df1>.iterrows( )
2015 2016 2017
qt1 30000 40000 60000

qt2 50000 50000 33000

qt3 45000 49000 55000

qt4 60000 20000 90000


Iterating over a DataFrame

<df1>.iteritems( )
2015 2016 2017

qt1 30000 40000 60000


qt2 50000 50000 33000
qt3 45000 49000 55000
60000 20000 90000
qt4
Program to extract data from dataframe row wise:

In[1]:import pandas as pd
In[2]: disales={2015 : {'qt1' : 30000,'qt2‘:50000,
'qt3':45000, 'qt4':60000}, 2016:{'qt1':
40000,'qt2':50000,'qt3':49000,'qt4':20000},
2017: {'qt1' :60000,'qt2‘:33000,
'qt3':55000, 'qt4':90000}}
In[3]:df1=pd.DataFrame(disales)
Program to extract data from dataframe row wise:

In[4]: for(row,rowSeries) in df1.iterrows():


print ("Row Index:",row)
print ("Containing :")
print(rowSeries)
Program to extract data from dataframe row wise:

Out[4]: Row Index: qt1


Containing :
2015 30000
2016 90000
2017 30000
Name: qt1, dtype: int64
Row Index: qt2
Containing :
2015 50000
2016 80000
2017 50000
Name: qt2, dtype: int64
Program to extract data from dataframe row wise:

Row Index: qt3


Containing :
2015 45000
2016 70000
2017 45000
Name: qt3, dtype: int64
Row Index: qt4
Containing :
2015 60000
2016 66000
2017 60000
Name: qt4, dtype: int64
Program to extract data from dataframe column wise:

In[4]: for(col,colSeries) in df1.iteritems():


print (“Column Index:“,col)
print ("Containing :")
print(colSeries)
Program to extract data from dataframe column wise:

Out[4]: Column Index: 2015


Containing :
qt1 30000
qt2 50000
qt3 45000
qt4 60000
Name: 2015, dtype: int64
Column Index: 2016
Containing :
qt1 40000
qt2 50000
qt3 49000
qt4 20000
Name: 2016, dtype: int64
Program to extract data from dataframe column wise:

Column Index: 2017


Containing :
qt1 60000
qt2 33000
qt3 55000
qt4 90000
Name: 2017, dtype: int64

You might also like