If your are looking for the answer “how to iterate over rows in a DataFrame in Pandas”. So you are at the right place. This article covers the answer to your question. In this article we are going to cover different ways or methods to answer the question “how to iterate over rows in a DataFrame in Pandas”. Let’s see how to iterate over rows in a DataFrame in Pandas.


How to iterate over rows in a DataFrame in Pandas

In pandas, there are numerous ways to iterate over rows of a DataFrame or Series, each with its own set of advantages and disadvantages.

Method 1: By using the index attribute of the DataFrame.

The index property returns the index information of the DataFrame.

The index information contains the labels of the rows. If the rows has NOT named indexes, the index property returns a RangeIndex object with the start, stop, and step values.

# import pandas package as pd
import pandas as pd

# Define a dictionary containing students information
data = {'Name': ['Rahul', 'Pooja',
                'Ketan', 'Mili'],
        'RollNo': [101, 102, 103, 104],
        'Subject': ['Hindi', 'English',
                'Math', 'Science'],
        'Marks': [79, 85, 89, 95]}

# Convert the dictionary into DataFrame
df = pd.DataFrame(data, columns=['Name', 'RollNo',
                                'Subject', 'Marks'])

print("Given Dataframe :\n", df)

print("\nIterating over rows using index attribute :\n")

# iterate through each row and select
# 'Name' and 'Subject' column respectively.
for ind in df.index:
    print(df['Name'][ind], df['Subject'][ind])

Output:

Given Dataframe :
        Name   RollNo  Subject       Marks
0      Rahul   101       Hindi          79
1      Pooja   102     English          85
2      Ketan   103        Math          89
3       Mili   104     Science          95

Iterating over rows using index attribute :

Rahul Hindi
Pooja English
Ketan Math
Mili Science

Method 2: By using loc[] function of the DataFrame.

Access a group of rows and columns by label(s) or a boolean array.

.loc[] is primarily label based, but may also be used with a boolean array.

# import pandas package as pd
import pandas as pd

# Define a dictionary containing students data
data = {'Name': ['Rahul', 'Pooja',
                'Ketan', 'Mili'],
        'RollNo': [101, 102, 103, 104],
        'Subject': ['Hindi', 'English',
                'Math', 'Science'],
        'Marks': [79, 85, 89, 95]}

# Convert the dictionary into DataFrame
df = pd.DataFrame(data, columns=['Name', 'RollNo',
                                'Subject', 'Marks'])

print("Given Dataframe :\n", df)

print("\nIterating over rows using loc function :\n")

# iterate through each row and select
# 'Name' and 'RollNo' column respectively.
for i in range(len(df)):
    print(df.loc[i, "Name"], df.loc[i, "RollNo"])

Output:

Given Dataframe :
        Name   RollNo  Subject       Marks
0      Rahul   101       Hindi          79
1      Pooja   102     English          85
2      Ketan   103        Math          89
3       Mili   104     Science          95

Iterating over rows using loc function :

Rahul 101
Pooja 102
Ketan 103
Mili  104

Method 3: By using iloc[] function of the DataFrame.

.iloc[] is primarily integer position based (from 0 to length-1 of the axis), but may also be used with a boolean array.

# import pandas package as pd
import pandas as pd

# Define a dictionary containing students data
data = {'Name': ['Rahul', 'Pooja',
                'Ketan', 'Mili'],
        'RollNo': [101, 102, 103, 104],
        'Subject': ['Hindi', 'English',
                'Math', 'Science'],
        'Marks': [79, 85, 89, 95]}

# Convert the dictionary into DataFrame
df = pd.DataFrame(data, columns=['Name', 'RollNo',
                                'Subject', 'Marks'])

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   RollNo  Subject       Marks
0      Rahul   101       Hindi          79
1      Pooja   102     English          85
2      Ketan   103        Math          89
3       Mili   104     Science          95

Iterating over rows using iloc function :

Rahul Hindi
Pooja English
Ketan Math
Mili Science

Method 4: By using iterrows() method of the DataFrame.

DataFrame. iterrows() method is used to iterate over DataFrame rows as (index, Series) pairs. Note that this method does not preserve the dtypes across rows due to the fact that this method will convert each row into a Series. The iterrows() method is the simplest way to iterate across rows, as shown below:

# import pandas package as pd
import pandas as pd

# Define a dictionary containing students data
data = {'Name': ['Rahul', 'Pooja',
                'Ketan', 'Mili'],
        'RollNo': [101, 102, 103, 104],
        'Subject': ['Hindi', 'English',
                'Math', 'Science'],
        'Marks': [79, 85, 89, 95]}

# Convert the dictionary into DataFrame
df = pd.DataFrame(data, columns=['Name', 'RollNo',
                                'Subject', 'Marks'])

print("Given Dataframe :\n", df)

print("\nIterating over rows using iterrows() method :\n")

# iterate through each row and select
# 'Name' and 'RollNo' column respectively.
for index, row in df.iterrows():
    print(row["Name"], row["RollNo"])

Output:

Given Dataframe :
        Name   RollNo  Subject       Marks
0      Rahul   101       Hindi          79
1      Pooja   102     English          85
2      Ketan   103        Math          89
3       Mili   104     Science          95

Iterating over rows using iterrows() method :

Rahul 101
Pooja 102
Ketan 103
Mili  104

Method 5: By using itertuples() method of the DataFrame.

itertuples() method will return an iterator yielding a named 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.

# import pandas package as pd
import pandas as pd

# Define a dictionary containing students data
data = {'Name': ['Rahul', 'Pooja',
                'Ketan', 'Mili'],
        'RollNo': [101, 102, 103, 104],
        'Subject': ['Hindi', 'English',
                'Math', 'Science'],
        'Marks': [79, 85, 89, 95]}

# Convert the dictionary into DataFrame
df = pd.DataFrame(data, columns=['Name', 'RollNo',
                                'Subject', 'Marks'])

print("Given Dataframe :\n", df)

print("\nIterating over rows using itertuples() method :\n")

# iterate through each row and select
# 'Name' and 'Marks' column respectively.
for row in df.itertuples(index=True, name='Pandas'):
    print(getattr(row, "Name"), getattr(row, "Marks"))

Output:

Given Dataframe :
        Name   RollNo  Subject       Marks
0      Rahul   101       Hindi          79
1      Pooja   102     English          85
2      Ketan   103        Math          89
3       Mili   104     Science          95

Iterating over rows using itertuples() method :

Rahul 79
Pooja 85
Ketan 89
Mili  95

Method 6: By using apply() method of the DataFrame.

The apply() method allows you to apply a function along one of the axis of the DataFrame, default 0, which is the index (row) axis.

# import pandas package as pd
import pandas as pd

# Define a dictionary containing students data
data = {'Name': ['Rahul', 'Pooja',
                'Ketan', 'Mili'],
        'RollNo': [101, 102, 103, 104],
        'Subject': ['Hindi', 'English',
                'Math', 'Science'],
        'Marks': [79, 85, 89, 95]}

# Convert the dictionary into DataFrame
df = pd.DataFrame(data, columns=['Name', 'RollNo',
                                'Subject', 'Marks'])

print("Given Dataframe :\n", df)

print("\nIterating over rows using apply function :\n")

# iterate through each row and concatenate
# 'Name' and 'Marks' column respectively.
print(df.apply(lambda row: row["Name"] + " " +
            str(row["Marks"]), axis=1))

Output:

Given Dataframe :
        Name   RollNo  Subject       Marks
0      Rahul   101       Hindi          79
1      Pooja   102     English          85
2      Ketan   103        Math          89
3       Mili   104     Science          95

Iterating over rows using apply function :

0        Rahul 79
1        Pooja 85
2        Ketan 89
3         Mili 95
dtype: object

Final Thoughts

When using dataframes for the first time, it is allowed to manipulate the rows using iterrows or itertuples . apply  is typically relatively simple to put into practice in place of iterrows, making it useful when you need a quick fix. But since real-world datasets can grow to enormous sizes, vectorized approaches (like  map) are frequently the best choice. As was previously said, vectorized methods scale significantly better when dealing with large datasets. When working in the data industry, vectorizing dataframe processing is crucial because good scalability prevents long runtimes with large datasets.