0

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:

  File "c:/Users/xxx/OneDrive - Universität Paderborn/Universität/SS_21/Python_Kurse/Hello-World.py", line 37, in <module>
    df.loc["axe"]
  File "C:\xxxx\miniconda3\lib\site-packages\pandas\core\indexing.py", line 895, in __getitem__
    return self._getitem_axis(maybe_callable, axis=axis)
  File "C:\xxxxx\miniconda3\lib\site-packages\pandas\core\indexing.py", line 1124, in _getitem_axis
    return self._get_label(key, axis=axis)
  File "C:\xxxx\miniconda3\lib\site-packages\pandas\core\indexing.py", line 1073, in _get_label
    return self.obj.xs(label, axis=axis)
  File "C:\xxxx\miniconda3\lib\site-packages\pandas\core\generic.py", line 3739, in xs
    loc = index.get_loc(key)
  File "C:\xxxx\miniconda3\lib\site-packages\pandas\core\indexes\range.py", line 354, in get_loc
    raise KeyError(key)
KeyError: 'axe'

1 Answer 1

2

You also need to give index like

df.loc[index,"sex"]

For example:

df.loc[1,"sex"]

If you only want to get sex column:

df["sex"]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.