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

1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.