Open In App

How to Fix: KeyError in Pandas

Last Updated : 28 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Pandas KeyError occurs when you try to access a column or row label in a DataFrame that doesn’t exist. This error often results from misspelled labels, unwanted spaces, or case mismatches. This article covers common causes of KeyError in pandas and how to fix them.

1. Column name not found

When working with a pandas DataFrame, you might try to access a column using syntax like df['Gender']. But if that column doesn't actually exist in the DataFrame, pandas will throw a KeyError.

Python
import pandas as pd

df = pd.DataFrame({'Name': ['Alice', 'Bob'], 'Age': [25, 30]})
df['Gender'] 

Output

KeyError: 'Gender'

Solution: Before trying to access a column, it’s a good practice to check if it exists using the in operator. Here's a safe way to handle it:

Python
import pandas as pd
df = pd.DataFrame({'Name': ['Alice', 'Bob'], 'Age': [25, 30]})
print(df.columns)

if 'Gender' in df.columns:
    print(df['Gender'])
else:
    print("Column not exist.")

Output

Index(['Name', 'Age'], dtype='object')
Column not exist.

Explanation: This code safely accesses a column by first printing all column names with df.columns, then checking if 'Gender' exists using the in operator. If it exists, it prints the values otherwise, it shows "Column not exist."

2. Extra spaces in column names

Sometimes a column might seem like it exists, but pandas can't find it because it includes unintended spaces (e.g., ' Name' instead of 'Name'). This usually happens when reading data from CSV or Excel files.

Python
import pandas as pd

df = pd.DataFrame({' Name': ['Alice', 'Bob'], 'Age': [25, 30]})
df['Name']

Output

 KeyError: 'Name'

Solution: Use .str.strip() to remove any leading/trailing spaces from column names before accessing them.

Python
import pandas as pd
df = pd.DataFrame({' Name': ['Alice', 'Bob'], 'Age': [25, 30]})

df.columns = df.columns.str.strip()
df['Name']

Output

Output
Using strip()

Explanation: It first creates a DataFrame with a column named ' Name' (with a leading space). Then, it removes any spaces from the column names using df.columns.str.strip(). After that, accessing the column 'Name' works correctly without causing a KeyError.

3. Accessing row with invalid label using iloc[]

If you try to access a row label using df.loc['c'], and 'c' does not exist in the index, pandas raises a KeyError.

Python
import pandas as pd

df = pd.DataFrame({'Name': ['Alice', 'Bob']}, index=['a', 'b'])
df.loc['c']

Output

KeyError: 'c'

Solution: Always check if the row label exists in the DataFrame's index before accessing it.

Python
import pandas as pd

df = pd.DataFrame({' Name': ['Alice', 'Bob'], 'Age': [25, 30]})
print(df.index)

if 'c' in df.index:
    print(df.loc['c'])
else:
    print("Row label not exist.")

Output
RangeIndex(start=0, stop=2, step=1)
Row label not exist.

Explanation: It first prints all row labels using df.index. Then, it uses the in operator to see if the label 'c' is present. If the label exists, it prints the corresponding row otherwise, it shows the message "Row label not exist.".

4. Multiple columns access with at least one missing

If you try to access multiple columns at once (e.g., df[['Name', 'Gender']]) and even one of them is missing, pandas will throw a KeyError.

Python
import pandas as pd

df = pd.DataFrame({'Name': ['Alice', 'Bob']}, index=['a', 'b'])
df[['Name', 'Gender']]  

Output

KeyError: "['Gender'] not in index"

Solution: Filter out any columns that don't exist before accessing them.

Python
import pandas as pd
df = pd.DataFrame({'Name': ['Alice', 'Bob']}, index=['a', 'b'])

cols = [col for col in ['Name', 'Gender'] if col in df.columns]
df[cols]

Output

Output

Explanation: It creates a list cols that includes only the column names that actually exist in the DataFrame by checking each desired column with the in operator. Then, it accesses and prints only those existing columns, avoiding a KeyError caused by missing columns like 'Gender'.

5. Case sensitivity

Pandas column names are case-sensitive. Trying to access df['name'] when the actual column is Name will result in a KeyError.

Python
import pandas as pd

df = pd.DataFrame({'Name': ['Alice']})
df['name']

Output

KeyError: 'name'

Solution: Convert all column names to lowercase (or uppercase) using .str.lower() for consistent access.

Python
import pandas as pd
df = pd.DataFrame({'Name': ['Alice']})

df['Name']
df.columns = df.columns.str.lower()
df['name']

Output

Output
Using lower()

Explanation: Initially, it accesses the column 'Name' with the exact case. Then, it converts all column names to lowercase using str.lower(), allowing you to access the same column using the lowercase name 'name' without causing a KeyError.


Next Article
Article Tags :
Practice Tags :

Similar Reads