Reputation: 9658
I have a DataFrame, from which I want to select a cell. I can select a cell by a row index and column label, but when I filter dataframe, same selection doesn't work.
print("Title:",df.loc[1,'title']) # Has no error
mobiles = df.loc[df['cat3']=='mobile-phones']
print("Title:",mobiles.loc[1,'title']) # Has error
I get the following error for the last print:
KeyError: 'the label [1] is not in the [index]'
Upvotes: 2
Views: 844
Reputation: 75080
When you assign mobiles as:
mobiles = df.loc[df['cat3']=='mobile-phones']
chances are there that df['cat3']=='mobile-phones'
met the condition at indexes which is not 1.
Use:
mobiles = df.loc[df['cat3']=='mobile-phones'].reset_index(drop=True)
Or you can use .iloc[]
to filter the first index(which doesnt see the label name)
Upvotes: 2