Python Tutorial: Pandas DataFrame-Iterating Exercises

Monday, 2 November 2020

Pandas DataFrame-Iterating Exercises

1.       Using iterrows() to extract dataframe row wise.

Solution:

import pandas as pd

Sales={'Year1':{'Qtr1':43000,'Qtr2':76000,'Qtr3':76000},\

       'Year2':{'Qtr1':47000,'Qtr2':60000,'Qtr3':96000},\

       'Year3':{'Qtr1':63000,'Qtr2':36000,'Qtr3':46000}}

df1=pd.DataFrame(Sales)

for (row, rowSales) in df1.iterrows():

    print("Row index : ", row)

    print("Containg :")

    print(rowSales)

    print()

Output :

Row index :  Qtr1

Containg :

Year1    43000

Year2    47000

Year3    63000

Name: Qtr1, dtype: int64

 

Row index :  Qtr2

Containg :

Year1    76000

Year2    60000

Year3    36000

Name: Qtr2, dtype: int64

 

Row index :  Qtr3

Containg :

Year1    76000

Year2    96000

Year3    46000

Name: Qtr3, dtype: int64

2.       Using iterrows() to extract row-wise Series objects.

Solution:

import pandas as pd

Sales={'Year1':{'Qtr1':43000,'Qtr2':76000,'Qtr3':76000},\

       'Year2':{'Qtr1':47000,'Qtr2':60000,'Qtr3':96000},\

       'Year3':{'Qtr1':63000,'Qtr2':36000,'Qtr3':46000}}

df1=pd.DataFrame(Sales)

for (row, rowSales) in df1.iterrows():

    print("Row index : ", row)

    print("Containg :")

    i=0

    for val in rowSales:

        print("At",i,"position:", val)

        i=i+1

    Output:

Row index :  Qtr1

Containg :

At 0 position: 43000

At 1 position: 47000

At 2 position: 63000

Row index :  Qtr2

Containg :

At 0 position: 76000

At 1 position: 60000

At 2 position: 36000

Row index :  Qtr3

Containg :

At 0 position: 76000

At 1 position: 96000

At 2 position: 46000

3.       Using iteritems() to extract data from dataframe columns wise.

Solution:

import pandas as pd

Sales={'Year1':{'Qtr1':43000,'Qtr2':76000,'Qtr3':76000},\

       'Year2':{'Qtr1':47000,'Qtr2':60000,'Qtr3':96000},\

       'Year3':{'Qtr1':63000,'Qtr2':36000,'Qtr3':46000}}

df1=pd.DataFrame(Sales)

for (Column, colSales) in df1.iteritems():

    print("Column index : ", Column)

    print("Containg :")

    print(colSales)

    print()

Output:

Column index :  Year1

Containg :

Qtr1    43000

Qtr2    76000

Qtr3    76000

Name: Year1, dtype: int64

 

Column index :  Year2

Containg :

Qtr1    47000

Qtr2    60000

Qtr3    96000

Name: Year2, dtype: int64

 

Column index :  Year3

Containg :

Qtr1    63000

Qtr2    36000

Qtr3    46000

Name: Year3, dtype: int64

4.       Using iteritems() to extract column wise Series objects.

Solution:

import pandas as pd

Sales={'Year1':{'Qtr1':43000,'Qtr2':76000,'Qtr3':76000},\

       'Year2':{'Qtr1':47000,'Qtr2':60000,'Qtr3':96000},\

       'Year3':{'Qtr1':63000,'Qtr2':36000,'Qtr3':46000}}

df1=pd.DataFrame(Sales)

for (Column, ColumnSales) in df1.iteritems():

    print("Column index : ", Column)

    print("Containg :")

    i=0

    for val in ColumnSales:

        print("At",i,"position:", val)

        i=i+1

    Output:

Column index :  Year1

Containg :

At 0 position: 43000

At 1 position: 76000

At 2 position: 76000

Column index :  Year2

Containg :

At 0 position: 47000

At 1 position: 60000

At 2 position: 96000

Column index :  Year3

Containg :

At 0 position: 63000

At 1 position: 36000

At 2 position: 46000

5.       Write a program to print the DataFrame df, one row at a time.

Solution:

import pandas as pd

student={"Name":["Ansu","Aviral","Kanha","Mritunjay"],\

      "Marks":[54,76,98,43]}

df1=pd.DataFrame(student,index=['Rno.1','Rno.2','Rno.3','Rno.4'])

for i, j in df1.iterrows():

    print(j)

    print("_____________")

Output:

Name     Ansu

Marks      54

Name: Rno.1, dtype: object

_____________

Name     Aviral

Marks        76

Name: Rno.2, dtype: object

_____________

Name     Kanha

Marks       98

Name: Rno.3, dtype: object

_____________

Name     Mritunjay

Marks           43

Name: Rno.4, dtype: object

_____________   

6.       Write a program to print DataFrame df, one column at a time.

Solution:

import pandas as pd

student={"Name":["Ansu","Aviral","Kanha","Mritunjay"],\

      "Marks":[54,76,98,43]}

df1=pd.DataFrame(student,index=['Rno.1','Rno.2','Rno.3','Rno.4'])

print("Student Information:\n",df1)

print()

for i, j in df1.iteritems():

    print(j)

    print("_____________")

    Output:

Student Information:

             Name  Marks

Rno.1       Ansu     54

Rno.2     Aviral     76

Rno.3      Kanha     98

Rno.4  Mritunjay     43

 

Rno.1         Ansu

Rno.2       Aviral

Rno.3        Kanha

Rno.4    Mritunjay

Name: Name, dtype: object

_____________

Rno.1    54

Rno.2    76

Rno.3    98

Rno.4    43

Name: Marks, dtype: int64

  

7.       Write a program to print only the values from marks column, for each row.

Solution:

import pandas as pd

student={"Name":["Ansu","Aviral","Kanha","Mritunjay"],\

      "Marks":[54,76,98,43]}

df1=pd.DataFrame(student,index=['Rno.1','Rno.2','Rno.3','Rno.4'])

print("Student Information:\n",df1)

print()

for i, row in df1.iterrows():

    print(row['Marks'])

    print("===========")

 

    Output:

Student Information:

             Name  Marks

Rno.1       Ansu     54

Rno.2     Aviral     76

Rno.3      Kanha     98

Rno.4  Mritunjay     43

 

54

=====================

76

=====================

98

=====================

43

=====================

 

 

No comments:

Post a Comment