Pandas KeyError is frustrating. This error happens because Pandas cannot find what you’re looking for.
To fix this either:
- Preferred Option: Make sure that your column label (or row label) is in your dataframe!
- Error catch option: Use df.get(‘your column’) to look for your column value. No error will be thrown if it is not found.
1. df.get('your_column', default=value_if_no_column)
Pseudo code: Check to see if a column is in your dataframe, if not, return the default value.
Pandas KeyError

In most cases, think of ‘key’ as the same as ‘name.’ Pandas is telling you that it can not find your column name. The preferred method is to *make sure your column name is in your dataframe.*
OR if you want to try and catch your error, you can use df.get(‘your_column’). However, if you don’t know what columns are in your dataframe…do you really know your data?
It’s best to head back upstream with your code and debug where your expectations and dataframe columns mismatch.
Try, Except
For a general solution, you can use the Try Except
convention to catch errors in your code. However, beware. Using a blanket Try/Except clause is dangerous and poor code practice if you do not know what you are doing.
If you are ‘catching’ general errors with try/except, this means that anything can slip through your code. This could result in unexpected errors getting through and a web of complexity.
Goal: Try to never let the reality of your code get too far away from the expectations of your code.
Let’s take a look at a sample:
import pandas as pd
Pandas KeyError¶
Pandas KeyError can be annoying. It generally happens when pandas cannot find the thing you're looking for. Usually this is to due a column it cannot find. It's simple to debug!
Let's check out some examples:
- Locating the error
- Fixing the error via the root cause
- Catching the error with df.get()
First, let's create a DataFrame
df = pd.DataFrame([('Foreign Cinema', 'Restaurant'),
('Liho Liho', 'Restaurant'),
('500 Club', 'bar'),
('The Square', 'bar')],
columns=('name', 'type')
)
df
Now let's try to call a column that is in our dataframe and is NOT in our dataframe
# Is in our dataframe
df['name']
# Is not in our dataframe
df['food']
Oh no! We got a KeyError. This means that Pandas cannot find "food" within our dataframe. We know why this is. Simply, it's not in our DF.
To get around this, either add a 'food' column. Or use df.get() to try and catch it. Here we will use .get() and notice there is no error thrown.
df.get('food')
This error can also happen when you're trying to access an index (for rows) label that doesn't exist. Check out this example
# Does exist
df.loc[2]
# Does not
df.loc[9]
This example immediately jumps out to me and says "hey, I can not find the label 9 in your row. Do something about this"
If you wanted to programatically do this, the long way would be to check for it first. If it is in the index, then proceed. But usually, I want to make sure I'm calling something I KNOW is in my index.
value = 9
if value in df.index:
print(df.loc[value])
else:
print("Not in index")
value = 2
if value in df.index:
print(df.loc[value])
else:
print("Not in index")
Check out more Pandas functions on our Pandas Page