Support Our Site

To ensure we can continue delivering content and maintaining a free platform for all users, we kindly request that you disable your adblocker. Your contribution greatly supports our site's growth and development.

How to Iterate Over Rows in a Dataframe in Pandas

2 min read

Pandas is a powerful library for working with data in Python, and the DataFrame is one of its most widely used data structures. One common task when working with DataFrames is to iterate over the rows and perform some action on each row.

Here are a few different approaches for iterating over rows in a DataFrame in Pandas:

1. Using the iterrows()

This method returns an iterator that yields index and row data as a tuple for each row. The row data is represented as a Pandas Series.

Here is an example of how to use the iterrows() method:

import pandas as pd

df = pd.read_csv('data.csv')
for index, row in df.iterrows():
    print(row['column_name'])

2. Using the itertuples()

This method returns an iterator that yields namedtuples of the rows. The namedtuples have fields corresponding to the column names. This method is generally faster than iterrows() as it doesn't construct a new Pandas Series for each row.

Here is an example of how to use the itertuples() method:

import pandas as pd

df = pd.read_csv('data.csv')
for row in df.itertuples():
    print(row.column_name)

3. Using the apply()

This method applies a function to each row or column of the DataFrame. The function can be passed as an argument and is applied to each row, and the results are combined into a new DataFrame.

Here is an example of how to use the apply() method to iterate over rows:

import pandas as pd

df = pd.read_csv('data.csv')
def my_function(row):
    print(row['column_name'])
df.apply(my_function, axis=1)

It's important to note that when working with large datasets, iterating over rows using iterrows() or a for loop can be slow, so itertuples() and apply() are better options performance wise.

In summary, there are several approaches to iterate over rows in a DataFrame in Pandas, and the best approach will depend on the specific needs of your project. The iterrows() and itertuples() methods are easy to use and understand, while apply() method provides more control over applying a specific function to each row and the for loop is the most basic method.


PYTHON

Latest Articles

Latest from djangocentral

How to Use Subquery() in Django With Practical Examples

In the realm of web development, Django stands as a powerful and versatile framework for building robust applications. One of the key aspects of developing efficient and optimized web applications is handling database queries effectively. In this article…
Read more →

4 min read

DRF Serializer: Handling OrderedDict and Converting It to a Dictionary or JSON

In Django Rest Framework (DRF) tests, when you access serializer.data, you might encounter an OrderedDict instead of a regular dictionary. This behavior is intentional and reflects the design of DRF's serialization process.Understanding the Problem The u…
Read more →

3 min read

Django Rest Framework CheetSheet: Mastering API Development

Django Rest Framework (DRF) is a powerful toolkit that makes building robust and scalable web APIs with Django a breeze. Whether you're a seasoned Django developer or a newcomer, having a comprehensive cheat sheet at your disposal can be a game-changer. …
Read more →

5 min read

How to Perform NOT Queries in Django ORM

In Django, performing NOT queries allows you to exclude certain records from the query results based on specific conditions. The NOT operator, represented by the tilde (~) when used in conjunction with the Django ORM's Q object, helps you construct compl…
Read more →

3 min read