In this tutorial, we'll take a look at the Pandas error:

KeyError:0

First, we'll create an example of how to reproduce it. Next, we'll explain the reason and finally, we'll see how to fix it.

Example

Let's work with the following DataFrame:

import pandas as pd

data={'day': [1, 2, 3, 4, 5],
     'numeric': [22, 222, '22K', '2M', '0.01 B']}

df = pd.DataFrame(data)

Data is:

day numeric
1 22
2 222
3 22K
4 2M
5 0.01 B

Reason

There are multiple reasons to get error like:

KeyError:0

Not existing column

One reason is trying to access column which don't exist:

df[0]

result in:

KeyError:0

Bad Input - Creating DataFrame with dict

Sometimes when we work with API we get dict as input. Some dict elements might cause similar problems.

In that case we need to drop elements from the input by:

data.pop('episode of')

In this case the element 'episode of' is pointing to an instance.

Working with IMDB library cinemagoer return result as:

{'title': 'The Iron Throne',
 'kind': 'episode',
 'episode of': <Movie id:0944947[http] title:_"Game of Thrones (TV Series 2011–2019) - IMDb" (None)_>,
 'season': 8,
 'episode': 6,
 'rating': 4.001234567891,
 'votes': 249455,
 'original air date': '19 May 2019',
 'year': '2019',
 'plot': "\nIn the aftermath of the devastating attack on King's Landing, Daenerys must face the survivors.    "}

Notice item:

 'episode of': <Movie id:0944947[http] title:_"Game of Thrones (TV Series 2011–2019) - IMDb" (None)_>,

This item cause error:

KeyError:0

Solution - wrong column

For the wrong column error we can check what are the current columns of the DataFrame:

df.columns

result:

Index(['day', 'numeric'], dtype='object')

Then we can access the correct one:

df['day']

Solution - bad input

To investigate and solve bad inputs which cause:

KeyError:0

We can try to isolate problematic values. For example creating DataFrame with line:

'episode of': <Movie id:0944947[http] title:_"Game of Thrones (TV Series 2011–2019) - IMDb" (None)_>,

will cause the error.

Dropping this line from the input by:

if 'episode of' in data.keys():
    data.pop('episode of')

will solve the error.