How to Loop over a Pandas DataFrame

2021-01-09

Intro

A common task when using pandas DataFrames is to loop over the rows. Pandas provides multiple ways for us to loop over dataframs depending on your needs. In this article, we will learn to loop over pandas arrays using iteritems, iterrows, and itertuples.

Looping over a DataFrame with iteritems

The first way to loop over a dataframe is to use the iteritems method. This iterator returns a tuple containing the column name and the pandas Series (row).

import pandas as pd

df = pd.DataFrame([
	{
		"person": "James",
		"sales": 1000,
	},
	{
		"person": "Clara",
		"sales": 3000,
	}
])

for col, item in df.iteritems():
    print(col)
	print(item)

Looping over a DataFrame with iterrows

The second way to loop over a dataframe is with the iterrows method. This iterator retuns the same as iteritems, a tuple containing the column name and the pandas Series.

import pandas as pd

df = pd.DataFrame([
	{
		"person": "James",
		"sales": 1000,
	},
	{
		"person": "Clara",
		"sales": 3000,
	}
])

for col, item in df.iterrows():
    print(col)
	print(item)

Looping over a DataFrame with itertuples

The third way to loop over a dataframe is using the itertuples method. Unlike the previous two methods, this will return each row as a Named Tuple.

import pandas as pd

df = pd.DataFrame([
	{
		"person": "James",
		"sales": 1000,
	},
	{
		"person": "Clara",
		"sales": 3000,
	}
])

for item in df.itertuples():
    print(item)
GoTea - KoalaTea