DataFrame.iterrows() - ( Pandas DataFrame Basics )
Heading h2
Syntax
for index, row in df.iterrows():
# Access row data using index and row variable
Example
import pandas as pd
df = pd.DataFrame({
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'Salary': [50000, 70000, 90000]
})
for index, row in df.iterrows():
print(f"{row['Name']} is {row['Age']} years old and earns {row['Salary']}")
Output
Alice is 25 years old and earns 50000
Bob is 30 years old and earns 70000
Charlie is 35 years old and earns 90000
Explanation
DataFrame.iterrows()
is a method in Pandas that allows us to iterate over rows of a dataframe. This method returns an iterator that produces a pair of the row index and the corresponding row data as a Series object. We can then access the row data using the index and the row variable.
In the above example, we have created a simple dataframe with 3 columns - 'Name', 'Age', and 'Salary'. We are then iterating over each row of the dataframe and printing out the values for the Name, Age, and Salary columns.
Use
DataFrame.iterrows()
is useful when we want to iterate over the rows of a dataframe and perform some operation on each row. For example, we may want to filter out some rows based on some condition, or modify the values of certain columns in the dataframe.
Important Points
DataFrame.iterrows()
iterates over the rows of a Pandas dataframe and returns an iterator that produces a pair of row index and row data- The row data is returned as a Series object
- We can access the row data using the index and row variable
DataFrame.iterrows()
can be used to perform operations on each row of a dataframe
Summary
In conclusion, DataFrame.iterrows()
is a useful method in Pandas that allows us to iterate over the rows of a dataframe. We can use it to perform various operations on each row of the dataframe. However, it should be noted that DataFrame.iterrows()
can be slow for large dataframes, and other approaches like apply()
or vectorized operations may be more efficient.