I have the following dataframe having "Location" and "Name" as the index.
Location Name Cost Item Purchased
Store 1 Chris 22.5 Dog Food
Kevyn 2.5 Kitty Litter
Store 2 Vinod 5.0 Bird Seed
I can access df.loc["Store 1"]
But df.loc["Store 1"]["Kevyn"] is giving me KeyError. What am I doing wrong?
You're using some incorrect chained indexing here, where you want to be indexing both levels of the MultiIndex with something like
df.loc['Store 1', 'Kevyn']
See Basic indexing on axis with MultiIndex in the docs.
You need to pass a tuple:
In [100]:
df.loc[('Store 1', 'Kevyn'),:]
Out[100]:
Cost 2.5
Item Purchased Kitty Litter
Name: (Store 1, Kevyn), dtype: object
The docs detail how to index
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