Articles by FavTutor
  • AI News
  • Data Structures
  • Web Developement
  • AI Code GeneratorNEW
  • Student Help
  • Main Website
No Result
View All Result
FavTutor
  • AI News
  • Data Structures
  • Web Developement
  • AI Code GeneratorNEW
  • Student Help
  • Main Website
No Result
View All Result
Articles by FavTutor
No Result
View All Result
Home Data Science

How to Iterate Over Rows in Pandas? (with Examples)

Piyush Kaushal by Piyush Kaushal
December 4, 2023
Reading Time: 4 mins read
Pandas iterate over rows
Follow us on Google News   Subscribe to our newsletter

One of the most common tasks in data analysis is to loop through rows of a Pandas DataFrame to perform various operations, like data manipulation and wrangling. In this article, we will discover different ways to iterate over rows of Pandas DataFrames.

What is a DataFrame?

Pandas DataFrames are a two-dimensional array with labeled data structures having different column types. It is a convenient way to work with structured data in Python. DataFrames are a standard and convenient way to store data in a tabular format, with rows to store the information and columns to name the information.

To learn about DataFrames in detail, you can check out how to Convert Pandas Series to DataFrame.

3 Ways to Iterate Over Rows in Pandas DataFrame

Looping through rows in a DataFrame allows us to access and manipulate the values of each row individually. Let us now explore the various methods to iterate over rows of a Pandas DataFrame:

1) Using the iterrows() Function

The pandas library in Python provides itterows() Function, which we can use to iterate over rows of a DataFrame. It returns an iterator that yields the index and a Series object containing the values of each row. By using the iterrows() function, we can get the values of every row using the column names as indices of the DataFrame.

The following Python code shows how to use iterrows() function to iterate over rows in Pandas DataFrame:

import pandas as pd

data = {
    "Name": ["John", "Emma", "Michael"],
    "Age": [45, 30, 55],
    "City": ["New York", "London", "Paris"]
}
df = pd.DataFrame(data)

# Display the original DataFrame
print('Original DataFrame:\n', df)

# Loop through rows using iterrows()
print('Iterated Rows:')
for index, row in df.iterrows():
    print(row['Name'], row['Age'], row['City'])

Output:

Original DataFrame:
       Name  Age      City
0     John   45  New York
1     Emma   30    London
2  Michael   55     Paris

Iterated Rows:
John 45 New York
Emma 30 London
Michael 55 Paris

2) Using List Comprehension

List comprehension is a brief and efficient way to iterate over rows in a DataFrame while performing operations on the data. We can just simply use a list comprehension to iterate over the values of a particular row. This technique is particularly useful when you need to perform calculations or transformations on the data while iterating.

Let us see how to do it in Python:

import pandas as pd

data = {
    "Name": ["John", "Emma", "Michael"],
    "Age": [45, 30, 55],
    "City": ["New York", "London", "Paris"]
}
df = pd.DataFrame(data)

# Display the original DataFrame
print('Original DataFrame:\n', df)

# Loop through rows using list comprehension
print('Iterated Rows:')
result = [row['Name'] for index, row in df.iterrows()]
print(result)

Output:

Original DataFrame:
       Name  Age      City
0     John   45  New York
1     Emma   30    London
2  Michael   55     Paris

Iterated Rows:
['John', 'Emma', 'Michael']

3) Using the apply() Function

One more way we can iterate over rows of Pandas DataFrame is by using the apply() function. The apply() function in Pandas is very productive as it can allow us to apply a function to each row or column of a DataFrame. This function can be defined using a lambda function or a custom-defined function.

Let us try an example:

import pandas as pd

data = {
    "Name": ["John", "Emma", "Michael"],
    "Age": [45, 30, 55],
    "City": ["New York", "London", "Paris"]
}
df = pd.DataFrame(data)

# Display the original DataFrame
print('Original DataFrame:\n', df)

# Use apply() with a lambda function
df['Name'] = df.apply(lambda row: row['Name'].upper(), axis=1)

# Display the new DataFrame. 
print('Updated DataFrame:\n',df)

Output:

Original DataFrame:
       Name  Age      City
0     John   45  New York
1     Emma   30    London
2  Michael   55     Paris

Updated DataFrame:
       Name  Age      City
0     JOHN   45  New York
1     EMMA   30    London
2  MICHAEL   55     Paris

Conclusion

In this article, we understood the various techniques and methods we can use to iterate over rows of a Pandas data frame. Now that you have a solid understanding of looping through Pandas, you can confidently apply your learnings from this article to your data analysis projects. If you need help with them, our Data Science Tutors are always available!

ShareTweetShareSendSend
Piyush Kaushal

Piyush Kaushal

I am Piyush Kaushal, currently pursuing a degree in software engineering at a prestigious government university. I am dedicated to staying informed about the latest technological developments and continuously expanding my knowledge base. I take great pleasure in sharing my expertise in data science and coding with fellow aspiring minds.

RelatedPosts

Moving Average in Pandas

Calculate Moving Average in Pandas (with code)

January 12, 2024
Pandas Convert Datetime to Date Column

Convert Datetime to Date Column in Pandas (with code)

January 4, 2024
Convert Pandas DataFrame to NumPy Array

Convert Pandas DataFrame to NumPy Array (with code)

January 3, 2024
Pandas DataFrame isna() Method

Pandas DataFrame isna() Method Explained

January 3, 2024
Pandas DataFrame copy() Method

Pandas DataFrame copy() Method Explained

January 1, 2024

About FavTutor

FavTutor is a trusted online tutoring service to connects students with expert tutors to provide guidance on Computer Science subjects like Java, Python, C, C++, SQL, Data Science, Statistics, etc.

Categories

  • AI News, Research & Latest Updates
  • Trending
  • Data Structures
  • Web Developement
  • Data Science

Important Subjects

  • Python Assignment Help
  • C++ Help
  • R Programming Help
  • Java Homework Help
  • Programming Help

Resources

  • About Us
  • Contact Us
  • Editorial Policy
  • Privacy Policy
  • Terms and Conditions

Website listed on Ecomswap. © Copyright 2025 All Rights Reserved.

No Result
View All Result
  • AI News
  • Data Structures
  • Web Developement
  • AI Code Generator
  • Student Help
  • Main Website

Website listed on Ecomswap. © Copyright 2025 All Rights Reserved.