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 index
And the v=df.loc[str(df['main_id']) == str(2456)]['code'].values
says 'KeyError False'
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With