How to loop (iterate) through every row of a pandas DataFrame ?


A way to iterate over rows of a pandas data frame in Python is by using the function iterrows(), items() or itertuples():

Let's consider the following pandas data frame:

>>> import pandas as pd
>>> data = {'Country':['USA', 'Spain', 'France', 'Canada'], 'Age':[10, 39, 21, 70]} 
>>> df = pd.DataFrame(data, index =['Bob', 'Jack', 'Ben', 'Paul'])
>>> df
      Age Country
Bob    10     USA
Jack   39   Spain
Ben    21  France
Paul   70  Canada

Using iterrows()

Pandas' DataFrame.iterrows() is a useful method for looping through each row of a Dataframe. It returns an iterator yielding each index value along with a series containing the data in each row. This allows you to easily access and modify the values in any given row.

>>> for index, row in df.iterrows():
...     print(index)
...     print(row)
... 
Bob
Age         10
Country    USA
Name: Bob, dtype: object
Jack
Age           39
Country    Spain
Name: Jack, dtype: object
Ben
Age            21
Country    France
Name: Ben, dtype: object
Paul
Age            70
Country    Canada
Name: Paul, dtype: object

Using items()

To get the variation of columns in function of the data frame indexes, a solution is to use items():

>>> for label, content in df.items():
...     print(label)
...     print(content)
... 
Age
Bob     10
Jack    39
Ben     21
Paul    70
Name: Age, dtype: int64
Country
Bob        USA
Jack     Spain
Ben     France
Paul    Canada
Name: Country, dtype: object

To focus on a given column:

>>> for label, content in df['Age'].items():
...     print(label,content)
... 
Bob 10
Jack 39
Ben 21
Paul 70

Using itertuples()

Another solution is to use itertuples():

>>> for row in df.itertuples():
...     print(row)
... 
Pandas(Index='Bob', Age=10, Country='USA')
Pandas(Index='Jack', Age=39, Country='Spain')
Pandas(Index='Ben', Age=21, Country='France')
Pandas(Index='Paul', Age=70, Country='Canada')

Example 2:

>>> for row in df.itertuples():
...     print(row[0],row[2])
... 
Bob USA
Jack Spain
Ben France
Paul Canada

Example 3:

>>> for row in df.itertuples():
...     print(row.Age)
... 
10
39
21
70

References