Javaexercise.com

How to Iterate Pandas DataFrame

To iterate over rows of dataframe, Pandas provides several built-in methods such as iterrows(), itertuples(), etc. In this article, we will see how to iterate or traverse the elements of dataframe. We use the following ways:

  1. iterrows() Function

  2. itertuples() Function

  3. range() Function + iloc[]

  4. for loop

  5. apply() Function

Let's get started and see the examples.

 

Iterate Dataframe rows by using iterrows() Function

Here, we used iterrows() function to traverse the elements of dataframe. This function returns pair of index and series that further can be iterate using the for loop. It is one of the easy ways to traverse the elements.

import pandas as pd

df = pd.DataFrame({
    'A': [12, 13, 14, 15],
    'B': [20, 40, 50, 30],
    'C': [-100, 40, -10, -80]
})

for index, row in df.iterrows():
    print(row['A'], row['B'], row['C'])

Output:

12 20 -100
13 40 40
14 50 -10
15 30 -80

 

Iterate Dataframe rows by using itertuples() Function

Here, we used itertuples() function to iterate the elements of the dataframe. This function returns an iterator that further can be used to fetch elements. See the below code example.

import pandas as pd

df = pd.DataFrame({
    'A': [12, 13, 14, 15],
    'B': [20, 40, 50, 30],
    'C': [-100, 40, -10, -80]
})

for row in df.itertuples(index=True, name='Pandas'):
    print(row.A, row.B, row.C)

Output:

12 20 -100
13 40 40
14 50 -10
15 30 -80

 

Iterate Dataframe rows by using range() and iloc[] in Python

Here, we used range() function in for loop and df.iloc[] which is an indexing system to fetch element from the specified location. 

import pandas as pd

df = pd.DataFrame({
    'A': [12, 13, 14, 15],
    'B': [20, 40, 50, 30],
    'C': [-100, 40, -10, -80]
})

for i in range(0, len(df)):
    print(df.iloc[i]['A'], df.iloc[i]['B'], df.iloc[i]['C'])

Output:

12 20 -100
13 40 40
14 50 -10
15 30 -80

 

Iterate Dataframe rows by using iterrows() and iteritems() in Python

In this example, we used iterrows() and iteritems() function in the loop, and two loops were used to traverse the elements into the row and column order. See the code.

import pandas as pd

df = pd.DataFrame({
    'A': [12, 13, 14, 15],
    'B': [20, 40, 50, 30],
    'C': [-100, 40, -10, -80]
})

for i, row in df.iterrows():
    for j, column in row.iteritems():
        print(column, "", end = '')
    print("\n")

Output:

12 20 -100 

13 40 40 

14 50 -10 

15 30 -80 

 

Iterate Dataframe rows by using itertuples() in Python

If you want to get each row as an array then you can use asarray() function of NumPy. See, we used the itertuples() function to iterate elements and then display them as an array by using the asarray() function.

import pandas as pd
import numpy as np

df = pd.DataFrame({
    'A': [12, 13, 14, 15],
    'B': [20, 40, 50, 30],
    'C': [-100, 40, -10, -80]
})

for row in df.itertuples(index=False, name='Pandas'):
    print(np.asarray(row))

Output:

[  12   20 -100]
[13 40 40]
[ 14  50 -10]
[ 15  30 -80]

 

Iterate Dataframe by using apply() function in Python

You can also use apply() function to traverse dataframe elements. In this example, we used apply() function that takes a lambda expression as an argument that again passes each row to the print_df() function. In this function, we print each element by using its index value. See the code below.

import pandas as pd

df = pd.DataFrame({
    'A': [12, 13, 14, 15],
    'B': [20, 40, 50, 30],
    'C': [-100, 40, -10, -80]
})

def print_df(row):
   print(row['A'], row['B'], row['C'])

df.apply(lambda row: print_df(row), axis=1)

Output:

12 20 -100
13 40 40
14 50 -10
15 30 -80