Pandas is a popular data manipulation library in Python that provides powerful tools for data analysis and manipulation. One of the common tasks when working with tabular data is iterating over rows to perform various operations or transformations. In this tutorial, we will explore different methods to iterate over rows in a Pandas DataFrame, along with practical examples to showcase their usage.

## Table of Contents

- Introduction to Pandas
- Iterating Over Rows in Pandas DataFrames

- Using
`iterrows()`

- Using List Comprehension

- Example 1: Calculating Row Sum
- Example 2: Applying Custom Function
- Performance Considerations
- Conclusion

## 1. Introduction to Pandas

Pandas is an open-source Python library that provides data structures and functions to efficiently manipulate and analyze data. Its primary data structures, the `Series`

and `DataFrame`

, are designed to handle various types of data and enable powerful data manipulation capabilities.

A `DataFrame`

is a two-dimensional, size-mutable, and heterogeneous tabular data structure with labeled axes (rows and columns). It’s often used to store and manipulate data in a structured manner, similar to a spreadsheet.

## 2. Iterating Over Rows in Pandas DataFrames

There are several ways to iterate over rows in a Pandas DataFrame. However, it’s important to note that Pandas is optimized for vectorized operations, which are generally faster than explicit row-wise iterations. Still, there might be scenarios where row-wise iteration is necessary.

### Using `iterrows()`

The `iterrows()`

function is provided by Pandas to iterate over rows in a DataFrame. It returns an iterator that produces pairs of index and Series for each row.

```
import pandas as pd
# Creating a sample DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 22]}
df = pd.DataFrame(data)
# Iterating over rows using iterrows()
for index, row in df.iterrows():
print(index, row['Name'], row['Age'])
```

### Using List Comprehension

List comprehension is another approach to iterate over rows in a DataFrame. It involves creating a list by iterating through rows and applying a specific operation.

```
# Iterating over rows using list comprehension
name_age_list = [(row['Name'], row['Age']) for _, row in df.iterrows()]
print(name_age_list)
```

## 3. Example 1: Calculating Row Sum

Let’s consider a practical example where we have a DataFrame containing sales data for different products, and we want to calculate the total sales for each row.

```
# Creating a sample DataFrame for sales data
sales_data = {'Product': ['Product A', 'Product B', 'Product C'],
'Quarter1': [1000, 1500, 800],
'Quarter2': [1200, 1300, 850],
'Quarter3': [900, 1600, 820]}
sales_df = pd.DataFrame(sales_data)
# Adding a new column for total sales
sales_df['Total_Sales'] = sales_df[['Quarter1', 'Quarter2', 'Quarter3']].sum(axis=1)
# Displaying the updated DataFrame
print(sales_df)
```

In this example, we first create a new column called ‘Total_Sales’ by summing the values of the ‘Quarter1’, ‘Quarter2’, and ‘Quarter3’ columns along the rows using the `sum()`

function with `axis=1`

.

## 4. Example 2: Applying Custom Function

In some cases, you might need to apply a custom function to each row of a DataFrame. Let’s say we have a DataFrame containing student information, and we want to assign grades based on their scores.

```
# Creating a sample DataFrame for student information
student_data = {'Name': ['Alice', 'Bob', 'Charlie'],
'Score': [85, 70, 92]}
student_df = pd.DataFrame(student_data)
# Function to assign grades
def assign_grade(score):
if score >= 90:
return 'A'
elif score >= 80:
return 'B'
elif score >= 70:
return 'C'
else:
return 'F'
# Applying the custom function to each row
student_df['Grade'] = student_df['Score'].apply(assign_grade)
# Displaying the updated DataFrame
print(student_df)
```

In this example, we define a custom function `assign_grade()`

that takes a student’s score as input and returns a grade based on predefined criteria. We then use the `apply()`

function to apply this custom function to each row in the DataFrame and create a new ‘Grade’ column.

## 5. Performance Considerations

While the methods discussed above work for iterating over rows in a DataFrame, it’s important to note that these methods can be relatively slower compared to vectorized operations. Pandas is optimized to perform operations on entire columns or subsets of data, which is often more efficient than row-wise iteration.

If possible, try to leverage vectorized operations and built-in functions to perform data manipulations. However, if row-wise iteration is necessary, consider using alternative methods like list comprehension for potentially improved performance.

## 6. Conclusion

Iterating over rows in a Pandas DataFrame can be accomplished using various methods, such as `iterrows()`

and list comprehension. These methods allow you to access and manipulate row data effectively. However, keep in mind that Pandas is optimized for vectorized operations, so it’s advisable to use row-wise iteration sparingly and consider alternative approaches for better performance.

In this tutorial, we explored two examples to illustrate the usage of row-wise iteration. We calculated row sums for sales data and applied a custom function to assign grades based on student scores. By understanding these techniques, you can efficiently work with individual rows of data within a Pandas DataFrame while leveraging the library’s data manipulation capabilities.