Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cannot encode categorical data

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'])

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading