In this article, we will cover how to iterate over rows in a DataFrame in Pandas.
How to iterate over rows in a DataFrame in Pandas
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
Let’s see the Different ways to iterate over rows in Pandas Dataframe :
Method 1: Using the index attribute of the Dataframe.
Python3
import pandas as pd
data = { 'Name' : [ 'Ankit' , 'Amit' ,
'Aishwarya' , 'Priyanka' ],
'Age' : [ 21 , 19 , 20 , 18 ],
'Stream' : [ 'Math' , 'Commerce' ,
'Arts' , 'Biology' ],
'Percentage' : [ 88 , 92 , 95 , 70 ]}
df = pd.DataFrame(data, columns = [ 'Name' , 'Age' ,
'Stream' , 'Percentage' ])
print ( "Given Dataframe :\n" , df)
print ( "\nIterating over rows using index attribute :\n" )
for ind in df.index:
print (df[ 'Name' ][ind], df[ 'Stream' ][ind])
|
Output:
Given Dataframe :
Name Age Stream Percentage
0 Ankit 21 Math 88
1 Amit 19 Commerce 92
2 Aishwarya 20 Arts 95
3 Priyanka 18 Biology 70
Iterating over rows using index attribute :
Ankit Math
Amit Commerce
Aishwarya Arts
Priyanka Biology
Method 2: Using loc[] function of the Dataframe.
Python3
import pandas as pd
data = { 'Name' : [ 'Ankit' , 'Amit' ,
'Aishwarya' , 'Priyanka' ],
'Age' : [ 21 , 19 , 20 , 18 ],
'Stream' : [ 'Math' , 'Commerce' ,
'Arts' , 'Biology' ],
'Percentage' : [ 88 , 92 , 95 , 70 ]}
df = pd.DataFrame(data, columns = [ 'Name' , 'Age' ,
'Stream' ,
'Percentage' ])
print ( "Given Dataframe :\n" , df)
print ( "\nIterating over rows using loc function :\n" )
for i in range ( len (df)):
print (df.loc[i, "Name" ], df.loc[i, "Age" ])
|
Output:
Given Dataframe :
Name Age Stream Percentage
0 Ankit 21 Math 88
1 Amit 19 Commerce 92
2 Aishwarya 20 Arts 95
3 Priyanka 18 Biology 70
Iterating over rows using loc function :
Ankit 21
Amit 19
Aishwarya 20
Priyanka 18
Method 3: Using iloc[] function of the DataFrame.
Python3
import pandas as pd
data = { 'Name' : [ 'Ankit' , 'Amit' ,
'Aishwarya' , 'Priyanka' ],
'Age' : [ 21 , 19 , 20 , 18 ],
'Stream' : [ 'Math' , 'Commerce' ,
'Arts' , 'Biology' ],
'Percentage' : [ 88 , 92 , 95 , 70 ]}
df = pd.DataFrame(data, columns = [ 'Name' , 'Age' ,
'Stream' , 'Percentage' ])
print ( "Given Dataframe :\n" , df)
print ( "\nIterating over rows using iloc function :\n" )
for i in range ( len (df)):
print (df.iloc[i, 0 ], df.iloc[i, 2 ])
|
Output:
Given Dataframe :
Name Age Stream Percentage
0 Ankit 21 Math 88
1 Amit 19 Commerce 92
2 Aishwarya 20 Arts 95
3 Priyanka 18 Biology 70
Iterating over rows using iloc function :
Ankit Math
Amit Commerce
Aishwarya Arts
Priyanka Biology
Python3
import pandas as pd
data = { 'Name' : [ 'Ankit' , 'Amit' ,
'Aishwarya' , 'Priyanka' ],
'Age' : [ 21 , 19 , 20 , 18 ],
'Stream' : [ 'Math' , 'Commerce' ,
'Arts' , 'Biology' ],
'Percentage' : [ 88 , 92 , 95 , 70 ]}
df = pd.DataFrame(data, columns = [ 'Name' , 'Age' ,
'Stream' , 'Percentage' ])
print ( "Given Dataframe :\n" , df)
print ( "\nIterating over rows using iterrows() method :\n" )
for index, row in df.iterrows():
print (row[ "Name" ], row[ "Age" ])
|
Output:
Given Dataframe :
Name Age Stream Percentage
0 Ankit 21 Math 88
1 Amit 19 Commerce 92
2 Aishwarya 20 Arts 95
3 Priyanka 18 Biology 70
Iterating over rows using iterrows() method :
Ankit 21
Amit 19
Aishwarya 20
Priyanka 18
Method 5: Using itertuples() method of the Dataframe.
Python3
import pandas as pd
data = { 'Name' : [ 'Ankit' , 'Amit' , 'Aishwarya' ,
'Priyanka' ],
'Age' : [ 21 , 19 , 20 , 18 ],
'Stream' : [ 'Math' , 'Commerce' , 'Arts' ,
'Biology' ],
'Percentage' : [ 88 , 92 , 95 , 70 ]}
df = pd.DataFrame(data, columns = [ 'Name' , 'Age' ,
'Stream' ,
'Percentage' ])
print ( "Given Dataframe :\n" , df)
print ( "\nIterating over rows using itertuples() method :\n" )
for row in df.itertuples(index = True , name = 'Pandas' ):
print ( getattr (row, "Name" ), getattr (row, "Percentage" ))
|
Output:
Given Dataframe :
Name Age Stream Percentage
0 Ankit 21 Math 88
1 Amit 19 Commerce 92
2 Aishwarya 20 Arts 95
3 Priyanka 18 Biology 70
Iterating over rows using itertuples() method :
Ankit 88
Amit 92
Aishwarya 95
Priyanka 70
Method 6: Using apply() method of the Dataframe.
Python3
import pandas as pd
data = { 'Name' : [ 'Ankit' , 'Amit' , 'Aishwarya' ,
'Priyanka' ],
'Age' : [ 21 , 19 , 20 , 18 ],
'Stream' : [ 'Math' , 'Commerce' , 'Arts' ,
'Biology' ],
'Percentage' : [ 88 , 92 , 95 , 70 ]}
df = pd.DataFrame(data, columns = [ 'Name' , 'Age' , 'Stream' ,
'Percentage' ])
print ( "Given Dataframe :\n" , df)
print ( "\nIterating over rows using apply function :\n" )
print (df. apply ( lambda row: row[ "Name" ] + " " +
str (row[ "Percentage" ]), axis = 1 ))
|
Output:
Given Dataframe :
Name Age Stream Percentage
0 Ankit 21 Math 88
1 Amit 19 Commerce 92
2 Aishwarya 20 Arts 95
3 Priyanka 18 Biology 70
Iterating over rows using apply function :
0 Ankit 88
1 Amit 92
2 Aishwarya 95
3 Priyanka 70
dtype: object
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 :
29 Sep, 2023
Like Article
Save Article