In Pandas, the iterrows()
method is often used to iterate over rows of a DataFrame. However, you might encounter a KeyError while using this method, which typically happens when you’re trying to access a column that does not exist in the DataFrame or is incorrectly referenced. This article will explain the causes of this error and how to avoid it.
The KeyError when using iterrows()
typically occurs due to one of the following reasons:
Here’s an example of how this error might occur:
import pandas as pd
# Create a DataFrame
df = pd.DataFrame({
'Name': ['John', 'Alice', 'Bob'],
'Age': [25, 30, 22],
'Gender': ['Male', 'Female', 'Male']
})
# Attempt to iterate using iterrows() and access a non-existent column
for index, row in df.iterrows():
print(row['Salary'])
Output:
KeyError: 'Salary'
In this example, the column ‘Salary’ does not exist in the DataFrame, which leads to a KeyError when attempting to access it.
To fix this error, you need to ensure that the column you are trying to access exists in the DataFrame. Here are a few approaches:
Before attempting to access a column, ensure that the column exists in the DataFrame:
# Check if 'Salary' exists in the DataFrame before accessing it
for index, row in df.iterrows():
if 'Salary' in row:
print(row['Salary'])
else:
print("Column 'Salary' not found.")
Output:
Column 'Salary' not found.
Ensure you’re accessing the correct columns. If the column exists, access it by its actual name:
# Correct column access
for index, row in df.iterrows():
print(row['Name']) # Access 'Name' column correctly
print(row['Age']) # Access 'Age' column correctly
Output:
John
25
Alice
30
Bob
22
Instead of using iterrows()
, you can use loc[]
for accessing specific rows and columns, which is more efficient and avoids common pitfalls with iterrows()
:
# Access rows and columns with loc[]
for index, row in df.iterrows():
print(df.loc[index, 'Name']) # Access 'Name' for the given row index
Output:
John
Alice
Bob
The KeyError in Pandas when using iterrows()
generally occurs due to incorrectly referencing a column that doesn’t exist or is misspelled. To resolve this error, ensure that the column names are correct, check if the column exists before accessing it, or consider using more efficient alternatives like loc[]
for row and column access.
Pandas: How to Access Columns by Name In Pandas, accessing columns by name is a…
Pandas: How to Access or Select Columns by Index, not by Name In Pandas, accessing…
Pandas: How to Access Row by Index In Pandas, you can access rows in a…
Pandas: How to Access a Column Using iterrows() In Pandas, iterrows() is commonly used to…
Pandas - How to Update Values in iterrows In Pandas, iterrows() is a popular method…
Pandas DataFrame KeyError: 0 - Trying to access column or index that does not exist…