How to loop over a DataFrame using Pandas

1 min readPandasDataDataFrame
7-Day Challenge

Land Your First Data Science Job

A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

Pandas gives you the ability to loop over columns and rows.

Here are the different ways to loop over columns or rows using the Pandas Library.

We define our DataFrame first

We define an example DataFrame.

import pandas as pd
df = pd.DataFrame({"firstname":["Caroline", "Sebastian", "Bob", "John"],
                   "lastname":["Jones", "Smith", "Di Caprio", "Lennon"],
                   "gpa":[4.1233, 2.242, 4.152,  5.923]})
We first define our DataFrame

Solution 1 : Loop over the columns name

Sometimes you might want to loop over the columns name. To perform any operation on the entirety of the DataFrame.

Here is how we loop over the column list of a DataFrame.

for col_name in df.columns:
	print(col_name)
We loop over a columns name and print the column name

Solution 2 : Loop over a specific column

Now vertically, you might want to loop over the values of a specific column.

This is how we do it.

for firstname in df["firstname"]:
	print(firstname)
We loop over a column and print the values

If you want to edit its records, I would rather use the solution 3.

Solution 3 : Loop over to edit a specific column

If you want to edit the column values, the fastest way is to apply a lambda function which performs the operation you want to apply on the column values.

df["gpa"] = df["gpa"].apply(lambda x: round(x,2))
We loop using the lambda function and round the GPA

Solution 4 : Loop over rows using apply

You might need to do an operation that combines two or more column values.

This is the method using apply. Watch out the axis=1.

df["fullname"] = df.apply(lambda row: f'{row["firstname"]} {row["lastname"]}', axis=1)
We loop over the rows using apply and axis=1

Solution 5 : Loop over rows using iterrows()

If you want to check the full row of a Pandas DataFrame that is the solution the you are looking for.

for idx, row in df.iterrows():
    print(f"{row['student']} : {row['gpa']}")
7-Day Challenge

Land Your First Data Science Job

A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

Free Newsletter

Master Data Science in Days, Not Months 🚀

Skip the theoretical rabbit holes. Get practical data science skills delivered in bite-sized lessons – Approach used by real data scientist. Not bookworms. 📚

Weekly simple and practical lessons
Access to ready to use code examples
Skip the math, focus on results
Learn while drinking your coffee

By subscribing, you agree to receive our newsletter. You can unsubscribe at any time.