Label Not In List and KeyError
Solution 1
df.loc['name']
raises a KeyError because name
is not in the index; it is in the columns. When you use loc
, the first argument is for index. You can use df['name']
or df.loc[:, 'name']
.
You can also pass boolean arrays to loc
(both for index and columns). For example,
df.loc[df['main_id']=='2456']
Out:
main_id name code
3 2456 Slim 94
You can still select a particular column for this, too:
df.loc[df['main_id']=='2456', 'code']
Out:
3 94
Name: code, dtype: int64
With boolean indexes, the returning object will always be a Series even if you have only one value. So you might want to access the underlying array and select the first value from there:
df.loc[df['main_id']=='2456', 'code'].values[0]
Out:
94
But better way is to use the item
method:
df.loc[df['main_id']=='2456', 'code'].item()
Out:
94
This way, you'll get an error if the length of the returning Series is greater than 1 while values[0]
does not check that.
Solution 2
Alternative solution:
In [76]: df.set_index('main_id').at['2456','code']
Out[76]: 94
Danny W
Updated on June 13, 2022Comments
-
Danny W almost 2 years
I'm trying to get the value of a specific cell.
main_id name code 0 1345 Jones 32 1 1543 Jack 62 2 9874 Buck 86 3 2456 Slim 94
I want the cell that says code=94, as I already know the main_id but nothing else.
raw_data = {'main_id': ['1345', '1543', '9874', '2456'], 'name': ['Jones', 'Jack', 'Buck', 'Slim'], 'code': [32, 62, 86, 94]} df=pd.DataFrame(raw_data, columns = ['main_id', 'name', 'code']) v=df.loc[str(df['main_id']) == str(2456)]['code'].values print(df.loc['name'])
The
print(df.loc['name'])
claims the label is not in indexAnd the
v=df.loc[str(df['main_id']) == str(2456)]['code'].values
says 'KeyError False' -
Danny W almost 7 yearsThanks for your help guys, very helpful.
-
D.S. over 5 yearsI know this is an old question, but there is a speedier way to get first you need to set main_id as an index of the dataframe and then you can do this df.loc['2456'] and it is much faster than the approach suggested in the answer