0

I have created the below dataframe:

      C1     C2  Count
0   Jack  Item1     20
1  Jones  Item1     15

I am trying to search for the second row with person name Jones using the below command and trying to fetch the count 15.

person_inq="Jones"
item_inq="Item1"    
count_in_df = df.loc[(df['C1'] == person_inq) & (df['C2'] == item_inq),'Count'][0]

While executing the above command, i am getting key

KeyError: 0
vinzee
  • 17,022
  • 14
  • 42
  • 60
Padfoot123
  • 853
  • 1
  • 13
  • 26
  • `df.loc[(df['C1'] == person_inq) & (df['C2'] == item_inq),'Count'].item()` is what you want. – cs95 Dec 30 '17 at 08:54
  • Thank you.. yes item() worked. whats the difference between using [0] and item()? – Padfoot123 Dec 30 '17 at 08:58
  • The result of `loc` is a series slice. The series has index 1 and value 15. Now, If you try to extract `[0]`, it tries to look for the index "0" which doesn't exist in the index. [0] does not refer to the first value, unless you use `iloc` for positional indexing. So, `df.loc[(df['C1'] == person_inq) & (df['C2'] == item_inq),'Count'].iloc[0]` would have also worked. – cs95 Dec 30 '17 at 09:00
  • Great.. Thank you – Padfoot123 Dec 30 '17 at 09:01

0 Answers0