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 how to iterate over rows in Pandas Dataframe using iterrows() and itertuples() :
Method #1: Using the DataFrame.iterrows() method
This method iterated over the rows as (index, series) pairs.
Python3
import pandas as pd
input_df = [{ 'name' : 'Sujeet' , 'age' : 10 },
{ 'name' : 'Sameer' , 'age' : 11 },
{ 'name' : 'Sumit' , 'age' : 12 }]
df = pd.DataFrame(input_df)
print ( 'Original DataFrame: \n' , df)
print ( '\nRows iterated using iterrows() : ' )
for index, row in df.iterrows():
print (row[ 'name' ], row[ 'age' ])
|
Output:
Original DataFrame:
age name
0 10 Sujeet
1 11 Sameer
2 12 Sumit
Rows iterated using iterrows() :
Sujeet 10
Sameer 11
Sumit 12
Method #2: Using the DataFrame.itertuples() method
This method returns a named tuple for every row. getattr() function can be used to get the row attribute in the returned tuple. This method is faster than Method #1.
Python3
import pandas as pd
input_df = [{ 'name' : 'Sujeet' , 'age' : 10 },
{ 'name' : 'Sameer' , 'age' : 110 },
{ 'name' : 'Sumit' , 'age' : 120 }]
df = pd.DataFrame(input_df)
print ( 'Original DataFrame: \n' , df)
print ( '\nRows iterated using itertuples() : ' )
for row in df.itertuples():
print ( getattr (row, 'name' ), getattr (row, 'age' ))
|
Output:
Original DataFrame:
age name
0 10 Sujeet
1 110 Sameer
2 120 Sumit
Rows iterated using itertuples() :
Sujeet 10
Sameer 110
Sumit 120
There are few other ways we can iterate over rows in Pandas Dataframe. See Different ways to iterate over rows in Pandas Dataframe.
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 :
14 Jan, 2022
Like Article
Save Article