Articles

pandas DataFrame - iterate over rows | iterrows & itertuples pandas

pandas DataFrame - iterate over rows | iterrows & itertuples pandas


In this pandas tutorial, we will discuss about how to iterate over rows in pandas using:

  • iterate over pandas dataframe,
  • iterrows pandas dataframe,
  • pandas dataframe iterrows example,
  • itertuples pandas dataframe,
  • pandas dataframe itertuples example

DataFrame is an two dimensional data structure that will store data in two dimensional format. One dimension refers to a row and second dimension refers to a column, So It will store the data in rows and columns.

 

We can able to create this DataFrame using DataFrame() method. But this is available in pandas module, so we have to import pandas module.

Syntax:

pandas.DataFrame(data)

Where, data is the input dataframe. The data can be a dictionary that stores list of values with specified key

 

Example: Create DataFrame

In this example, we will create a dataframe with 4 rows and 4 columns with college data.

import pandas as pd

#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":[1200,3422,5644,670]

                   })

#display the dataframe
print(data)

Output:

  college_id       college_name college_address  Total Staff
0      c-001  vignan university          guntur         1200
1      c-021               vvit          guntur         3422
2      c-002           RVR - JC          guntur         5644
3      c-004  Andhra University          guntur          670

iterate over pandas dataframe

Method 1 : using iterrows pandas dataframe

Here, we will use iterrows() to iterate over the rows in the pandas dataframe.

 

We have to use for loop to iterate the rows using this method. It will take index and rows to iterate over the dataframe.

Finally, By using row, we can access the values by providing the column name.

Syntax:

for i, row in dataframe.iterrows():
    print (row["column"], ...............)

where,

  1. dataframe is the input pandas DataFrame.
  2. column is the input column
  3. i refers to the index iterator and row refers to the row iterator.

Examplepandas dataframe iterrows for loop

In this example, we will iterate the college_id and college_address columns in the dataframe using iterrows()

import pandas as pd

#create dataframe from the college data

data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":[1200,3422,5644,670]

                   })


# iterate the college_id and college_address columns in the dataframe using iterrows
for i, row in data.iterrows():
    print (row["college_id"], row["college_address"])

Outputpandas dataframe iterrows for loop result is given below

c-001 guntur
c-021 guntur
c-002 guntur
c-004 guntur

In the above pandas dataframe iterrows example code, we are goign to iterate the data from  college_id and college_address columns.


Method 2 : using pandas dataframe itertuples

Here, we will use itertuples() to iterate over the rows in the pandas dataframe.

 

We have to use for loop to iterate the rows using this method. It will take rows to iterate over the dataframe.

Finally, By using row, we can access the values by providing the column name that are passed inside getattr() method.

Syntax:

for row in dataframe.itertuples():
    print (getattr(row, "column"),.....................)

where,

  1. dataframe is the input pandas DataFrame.
  2. column is the input column
  3. row refers to the row iterator.

Examplepandas dataframe itertuples example

In this example, we will iterate the college_id and college_address columns in the dataframe using itertuples()

import pandas as pd

#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":[1200,3422,5644,670]

                   })

# iterate the college_id and college_address columns in the dataframe using itertuples
for row in data.itertuples():
    print (getattr(row, "college_id"), getattr(row, "college_address"))

Outputitertuples pandas dataframe result

c-001 guntur
c-021 guntur
c-002 guntur
c-004 guntur

In the above code, we are going to iterate the data from college_id and college_address columns.


Pandas

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University. Published 1200+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :    Published Date : Mar 13,2022  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!