Skip to content
Advertisement

KeyError when using loc from pandas

I have a simple CSV-file which I import as a dataframe. Using df.columns I get the following:

Index(['Unnamed: 0', 'age', 'sex', 'bmi', 'bp', 's1', 's2', 's3', 's4', 's5', 's6', 'target'], dtype='object')

So there definitely is a column named sex. When now using

df.loc["sex"]

I get the following output:

JavaScript

Advertisement

Answer

You also need to give index like

JavaScript

For example:

JavaScript

If you only want to get sex column:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement