I have a panel dataset about the Internet Penetration Rate of 11 countries and I want to encode the Country values.
Country Year Int Pen Rate % GDP Per Capita GDP Growth %
0 Australia 2014 84.000000 76864.54828 2.579017
1 Australia 2015 84.560515 77397.27020 2.152736
2 Australia 2016 86.540000 78278.37956 2.730548
3 Australia 2017 86.545049 78751.93511 2.282184
4 Australia 2018 90.000000 79813.73387 2.883045
Why do I get KeyError on Country when I use the following function?
series['Country'] = label_encoder.fit_transform(series['Country'])
I used sep=',' when I read the dataset.
>Solution :
I looks like you have some excess whitespaces (the "Country " is not right aligned).
For cleaning use df.columns = list(map(str.strip, df.columns)) and replace df with the name of your dataframe.