0

I'd like to figure out how I can create a column in my dataframe based on a multiple check condition.

When I use a single conditional check this seems to work fine.

df['1/1/2017'] = np.where(df["Term 1 Start Date"] <= '1/1/2017'), 'True', 'False')

However when I introduce a second option to check upon this fails telling me that ValueError: The truth value of a Series is ambiguous.

df['1/1/2017'] = np.where(
    (df["Term 1 Start Date"] <= '1/1/2017' and df["Term 1 End Date"] > '1/1/2017'), 'True', 'False')

It should be noted that not all lines are filled in. The dates contain dates as you would expect. What can I do to have this new column populate based on two criteria?

current dataframe

Desired result

Term 1 Start Date | 1/1/2017
blank | blank
6/12/2016 | True
5/1/2016 | True
2/1/2017 | False
4/1/2017 | False

cs95
  • 379,657
  • 97
  • 704
  • 746
mnickey
  • 727
  • 1
  • 6
  • 15
  • check the link https://stackoverflow.com/questions/21415661/logic-operator-for-boolean-indexing-in-pandas – BENY Aug 24 '17 at 22:22

1 Answers1

3

You'll need the bitwise operator & instead of and. You'll also need extra sets of parentheses to handle precedence.

df['1/1/2017'] = np.where(((df["Term 1 Start Date"] <= '1/1/2017')\ 
                           & (df["Term 1 End Date"] > '1/1/2017'),\
                     'True', 'False'))

Reason being, and doesn't play nice with dataframes, but the bitwise operators &, | and ~ are all overloaded for use in conditionals.

cs95
  • 379,657
  • 97
  • 704
  • 746