Issue
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
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.
Answered By - 7shoe
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.