Introduction

Pandas is an immensely popular data manipulation framework for Python. In a lot of cases, you might want to iterate over data - either to print it out, or perform some operations on it.

In this tutorial, we’ll take a look at how to iterate over rows in a Pandas DataFrame.

If you’re new to Pandas, you can read our beginner’s tutorial. Once you’re familiar, let’s look at the three main ways to iterate over DataFrame:

  • items()
  • iterrows()
  • itertuples()

Iterating DataFrames with items()

Let’s set up a DataFrame with some data of fictional people:

import pandas as pd

df = pd.DataFrame({
    'first_name': ['John', 'Jane', 'Marry', 'Victoria', 'Gabriel', 'Layla'],
    'last_name': ['Smith', 'Doe', 'Jackson', 'Smith', 'Brown', 'Martinez'],
    'age': [34, 29, 37, 52, 26, 32]},
    index=['id001', 'id002', 'id003', 'id004', 'id005', 'id006'])

Note that we are using id’s as our DataFrame’s index. Let’s take a look at how the DataFrame looks like:

print(df.to_string())
      first_name last_name  age
id001       John     Smith   34
id002       Jane       Doe   29
id003      Marry   Jackson   37
id004   Victoria     Smith   52
id005    Gabriel     Brown   26
id006      Layla  Martinez   32

Now, to iterate over this DataFrame, we’ll use the items() function:

df.items()

This returns a generator:

<generator object DataFrame.items at 0x7f3c064c1900>

We can use this to generate pairs of col_name and data. These pairs will contain a column name and every row of data for that column. Let’s loop through column names and their data:

for col_name, data in df.items():
	print("col_name:",col_name, "\ndata:",data)

This results in:

col_name: first_name
data: 
id001        John
id002        Jane
id003       Marry
id004    Victoria
id005     Gabriel
id006       Layla
Name: first_name, dtype: object
col_name: last_name
data: 
id001       Smith
id002         Doe
id003     Jackson
id004       Smith
id005       Brown
id006    Martinez
Name: last_name, dtype: object
col_name: age
data: 
id001    34
id002    29
id003    37
id004    52
id005    26
id006    32
Name: age, dtype: int64

#python #pandas

How to Iterate over Rows in a Pandas DataFrame
1.45 GEEK