How to iterate over rows in a DataFrame in Pandas?

 

You can iterate over rows in a Pandas DataFrame using various methods, but one common approach is to use the iterrows() method. Here's an example:

python
import pandas as pd # Create a sample DataFrame data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'], 'Age': [25, 30, 22, 28]} df = pd.DataFrame(data) # Iterate over rows using iterrows() for index, row in df.iterrows(): print(f"Index: {index}, Name: {row['Name']}, Age: {row['Age']}")

Output:

yaml
Index: 0, Name: Alice, Age: 25 Index: 1, Name: Bob, Age: 30 Index: 2, Name: Charlie, Age: 22 Index: 3, Name: David, Age: 28

In this example:

  • We use the iterrows() method to iterate over the rows of the DataFrame.
  • The index variable holds the index of the current row.
  • The row variable holds a Pandas Series containing the data of the current row.
  • We can access the values of specific columns using row['Column Name'].

Keep in mind that while iterrows() is a convenient way to iterate over rows, it might not be the most efficient method for large DataFrames. For better performance, consider using vectorized operations whenever possible.

Comments