Iteration is a general term for taking each item of something, one after another. Pandas DataFrame consists of rows and columns so, in order to iterate over dataframe, we have to iterate a dataframe like a dictionary. In a dictionary, we iterate over the keys of the object in the same way we have to iterate in dataframe.

In this article, we are using “nba.csv” file to download the CSV, click here.
In Pandas Dataframe we can iterate an element in two ways:
- Iterating over rows
- Iterating over columns
Iterating over rows :
In order to iterate over rows, we can use three function iteritems(), iterrows(), itertuples() . These three function will help in iteration over rows.
Iteration over rows using iterrows()
In order to iterate over rows, we apply a iterrows() function this function returns each index value along with a series containing the data in each row.
Code #1:
Python3
import pandas as pd
dict = { 'name' :[ "aparna" , "pankaj" , "sudhir" , "Geeku" ],
'degree' : [ "MBA" , "BCA" , "M.Tech" , "MBA" ],
'score' :[ 90 , 40 , 80 , 98 ]}
df = pd.DataFrame( dict )
print (df)
|
Now we apply iterrows() function in order to get a each element of rows.
Python3
import pandas as pd
dict = { 'name' :[ "aparna" , "pankaj" , "sudhir" , "Geeku" ],
'degree' : [ "MBA" , "BCA" , "M.Tech" , "MBA" ],
'score' :[ 90 , 40 , 80 , 98 ]}
df = pd.DataFrame( dict )
for i, j in df.iterrows():
print (i, j)
print ()
|
Output:

Code #2:
Python
import pandas as pd
data = pd.read_csv( "nba.csv" )
data.head( 3 )
|

Now we apply a iterrows to get each element of rows in dataframe
Python
import pandas as pd
data = pd.read_csv( "nba.csv" )
for i, j in data.iterrows():
print (i, j)
print ()
|
Output:

Iteration over rows using iteritems()
In order to iterate over rows, we use iteritems() function this function iterates over each column as key, value pair with the label as key, and column value as a Series object.
Code #1:
Python3
import pandas as pd
dict = { 'name' :[ "aparna" , "pankaj" , "sudhir" , "Geeku" ],
'degree' : [ "MBA" , "BCA" , "M.Tech" , "MBA" ],
'score' :[ 90 , 40 , 80 , 98 ]}
df = pd.DataFrame( dict )
print (df)
|
Now we apply a iteritems() function in order to retrieve an rows of dataframe.
Python3
import pandas as pd
dict = { 'name' :[ "aparna" , "pankaj" , "sudhir" , "Geeku" ],
'degree' : [ "MBA" , "BCA" , "M.Tech" , "MBA" ],
'score' :[ 90 , 40 , 80 , 98 ]}
df = pd.DataFrame( dict )
for key, value in df.iteritems():
print (key, value)
print ()
|
Output:

Code #2:
Python
import pandas as pd
data = pd.read_csv( "nba.csv" )
data.head( 3 )
|
Output:

Now we apply a iteritems() in order to retrieve rows from a dataframe
Python
import pandas as pd
data = pd.read_csv( "nba.csv" )
for key, value in data.iteritems():
print (key, value)
print ()
|
Output:

Iteration over rows using itertuples()
In order to iterate over rows, we apply a function itertuples() this function return a 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.
Code #1:
Python3
import pandas as pd
dict = { 'name' :[ "aparna" , "pankaj" , "sudhir" , "Geeku" ],
'degree' : [ "MBA" , "BCA" , "M.Tech" , "MBA" ],
'score' :[ 90 , 40 , 80 , 98 ]}
df = pd.DataFrame( dict )
print (df)
|
Now we apply a itertuples() function inorder to get tuple for each row
Python3
import pandas as pd
dict = { 'name' :[ "aparna" , "pankaj" , "sudhir" , "Geeku" ],
'degree' : [ "MBA" , "BCA" , "M.Tech" , "MBA" ],
'score' :[ 90 , 40 , 80 , 98 ]}
df = pd.DataFrame( dict )
for i in df.itertuples():
print (i)
|
Output:

Code #2:
Python
import pandas as pd
data = pd.read_csv( "nba.csv" )
data.head( 3 )
|

Now we apply an itertuples() to get atuple of each rows
Python
import pandas as pd
data = pd.read_csv( "nba.csv" )
for i in data.itertuples():
print (i)
|
Output:

Iterating over Columns :
In order to iterate over columns, we need to create a list of dataframe columns and then iterating through that list to pull out the dataframe columns.
Code #1:
Python3
import pandas as pd
dict = { 'name' :[ "aparna" , "pankaj" , "sudhir" , "Geeku" ],
'degree' : [ "MBA" , "BCA" , "M.Tech" , "MBA" ],
'score' :[ 90 , 40 , 80 , 98 ]}
df = pd.DataFrame( dict )
print (df)
|
Now we iterate through columns in order to iterate through columns we first create a list of dataframe columns and then iterate through list.
Python
columns = list (df)
for i in columns:
print (df[i][ 2 ])
|
Output:

Code #2:
Python
import pandas as pd
data = pd.read_csv( "nba.csv" )
col = data.head( 3 )
col
|

Now we iterate over columns in CSV file in order to iterate over columns we create a list of dataframe columns and iterate over list
Python
clmn = list (col)
for i in clmn:
print (col[i][ 2 ])
|
Output:

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
02 Jun, 2023
Like Article
Save Article