Different ways to iterate over rows in Pandas Dataframe

24 Jun, 2022
In this article, we will cover 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. 
Different Ways to Iterate Over Rows in Pandas DataFrame | GeeksforGeeks - YouTubeGeeksforGeeks530K subscribersDifferent Ways to Iterate Over Rows in Pandas DataFrame | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:002:59 / 9:26•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=mT-2AxZLtvw" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
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 package as pdimport pandas as pd  # Define a dictionary containing students datadata = {'Name': ['Ankit', 'Amit',                 'Aishwarya', 'Priyanka'],        'Age': [21, 19, 20, 18],        'Stream': ['Math', 'Commerce',                   'Arts', 'Biology'],        'Percentage': [88, 92, 95, 70]}  # Convert the dictionary into DataFramedf = pd.DataFrame(data, columns=['Name', 'Age',                                  'Stream', 'Percentage'])  print("Given Dataframe :\n", df)  print("\nIterating over rows using index attribute :\n")  # iterate through each row and select# 'Name' and 'Stream' column respectively.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
Python3
# import pandas package as pdimport pandas as pd  # Define a dictionary containing students datadata = {'Name': ['Ankit', 'Amit',                 'Aishwarya', 'Priyanka'],        'Age': [21, 19, 20, 18],        'Stream': ['Math', 'Commerce',                   'Arts', 'Biology'],        'Percentage': [88, 92, 95, 70]}  # Convert the dictionary into DataFramedf = pd.DataFrame(data, columns=['Name', 'Age',                                 'Stream',                                  'Percentage'])  print("Given Dataframe :\n", df)  print("\nIterating over rows using loc function :\n")  # iterate through each row and select# 'Name' and 'Age' column respectively.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
Python3
# import pandas package as pdimport pandas as pd  # Define a dictionary containing students datadata = {'Name': ['Ankit', 'Amit',                  'Aishwarya', 'Priyanka'],        'Age': [21, 19, 20, 18],        'Stream': ['Math', 'Commerce',                    'Arts', 'Biology'],        'Percentage': [88, 92, 95, 70]}  # Convert the dictionary into DataFramedf = pd.DataFrame(data, columns=['Name', 'Age',                                 'Stream', 'Percentage'])  print("Given Dataframe :\n", df)  print("\nIterating over rows using iloc function :\n")  # iterate through each row and select# 0th and 2nd index column respectively.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 package as pdimport pandas as pd  # Define a dictionary containing students datadata = {'Name': ['Ankit', 'Amit',                  'Aishwarya', 'Priyanka'],        'Age': [21, 19, 20, 18],        'Stream': ['Math', 'Commerce',                   'Arts', 'Biology'],        'Percentage': [88, 92, 95, 70]}  # Convert the dictionary into DataFramedf = pd.DataFrame(data, columns=['Name', 'Age',                                  'Stream', 'Percentage'])  print("Given Dataframe :\n", df)  print("\nIterating over rows using iterrows() method :\n")  # iterate through each row and select# 'Name' and 'Age' column respectively.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
Python3
# import pandas package as pdimport pandas as pd  # Define a dictionary containing students datadata = {'Name': ['Ankit', 'Amit', 'Aishwarya',                 'Priyanka'],        'Age': [21, 19, 20, 18],        'Stream': ['Math', 'Commerce', 'Arts',                    'Biology'],        'Percentage': [88, 92, 95, 70]}  # Convert the dictionary into DataFramedf = pd.DataFrame(data, columns=['Name', 'Age',                                  'Stream',                                 'Percentage'])  print("Given Dataframe :\n", df)  print("\nIterating over rows using itertuples() method :\n")  # iterate through each row and select# 'Name' and 'Percentage' column respectively.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
​
Python3
# import pandas package as pdimport pandas as pd  # Define a dictionary containing students datadata = {'Name': ['Ankit', 'Amit', 'Aishwarya',                 'Priyanka'],        'Age': [21, 19, 20, 18],        'Stream': ['Math', 'Commerce', 'Arts',                   'Biology'],        'Percentage': [88, 92, 95, 70]}  # Convert the dictionary into DataFramedf = pd.DataFrame(data, columns=['Name', 'Age', 'Stream',                                 'Percentage'])  print("Given Dataframe :\n", df)  print("\nIterating over rows using apply function :\n")  # iterate through each row and concatenate# 'Name' and 'Percentage' column respectively.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
surajkumarguptaintern
pandas-dataframe-program
Picked
Python pandas-dataFrame
Python-pandas
Technical Scripter 2018
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Iterate over a list in Python
Python OOPs Concepts
Convert integer to string in Python
Introduction To PYTHON