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

`df[df['name']=='Penny']` works well while `df[df['sn']=='016']` doesn't, how do I fix it?

I’m trying to select exact one row from a DataFrame with pandas.

Here is the DataFrame.

df = pd.DataFrame.from_dict({'final': {0: 100, 1: 100, 2: 100, 3: 95, 4: 95},
 'name': {0: 'Penny', 1: 'Sheldon', 2: 'Leonard', 3: 'Rajesh', 4: 'Howard'},
 'sn': {0: '016',
  1: '031',
  2: '016',
  3: '001',
  4: '007'}})
df.set_index('sn', inplace=True)

When I select the row with name, it works well

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

df[df['name']=='Penny']

However, sn

df[df['sn']=='016']

throws

KeyError: ‘sn’

Why is that, how do I fix it?

>Solution :

the reason is that sn represents the index of the Pandas Dataframe rather than a mere column, i.e.

df.index.name
> 'sn'

which leaves df.sn (or, equivalently, df['sn']) undefined throwing a KeyError: 'sn' when you attempt to access/set it.

Using df.index in general and df[df.index=='016'] in your particular example fixes the issue.

Note, however, that the index value 016 is assigned twice while Penny is only used once which leads to different subframes.

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