0

I have a dataframe input_data. I can't figure out how to select an individual row, based on two conditions. My dataframe looks as follows:

        Date   Open   High    Low  Close     Volume  Name
0 2006-01-03  77.76  79.35  77.24  79.11    3117200   ABC
1 2006-01-03  51.70  52.58  51.05  52.58    7825700   DEF

I can select by date alone, or name alone, but get errors with:

print(input_data.loc[(input_data['Date'] == pd.to_datetime('2006-01-03 00:00:00'))and (input_data['Name'] == 'ABC')]). 

I've tried to mimic the code from here, here and here but no luck.

print(input_data.loc[input_data['Date'] == pd.to_datetime('2006-01-03 00:00:00')]) 
print(input_data.loc[input_data['Name'] == 'ABC'])

# Below line doesn't work
print(input_data.loc[(input_data['Date'] == pd.to_datetime('2006-01-03 00:00:00'))and (input_data['Name'] == 'MMM')]) 

Using the date and 'ABC' I want to end up with:

0 2006-01-03  77.76  79.35  77.24  79.11    3117200   ABC

Simple answers that I can understand are preferred.

Arkistarvh Kltzuonstev
  • 6,824
  • 7
  • 26
  • 56
Mike
  • 201
  • 2
  • 14
  • Use `&` not `and` – ALollz Aug 22 '19 at 18:31
  • Thanks ALollz. Googling tells me that the difference was because 'and' is boolean, while '&' is bitwise, or vice versa. Setting A = 1 and B = 2, and printing out A and B or A & B demonstrates I don't really understand this, but thanks for your correction. – Mike Aug 22 '19 at 19:06
  • If you read the solutions in the duplicate they go into extensive depth trying to explain the difference. – ALollz Aug 22 '19 at 19:08

0 Answers0