Shantanu
Shantanu

Reputation: 867

How to use multiple if conditions for Pandas?

I am trying to use multiple conditions in my pandas, but I am getting the following error:

ValueError: The truth value of a Series is ambiguous. Use a.empty, 
a.bool(), a.item(), a.any() or a.all().

As per this thread I replaced the and with a bitwise operator, but I am still getting the error.

import pandas as pd

d1 = {'Year': [2019,2019,2019,2019,2019], 'Week':[1,2,4,6,7], 'Value': 
[20,40,60,75,90]}

df1 = pd.DataFrame(data=d1)

if (df1['Year'] == df1['Year'].shift(-1)) & \
 (df1['Week'] == df1['Week'].shift(-1)):
    print('Yes')
else:
    print('No')

What might I be doing wrong here?

Upvotes: 1

Views: 1000

Answers (3)

yatu
yatu

Reputation: 88226

You could use np.where which will yield Yes or No according to whether the condition is met or not:

c1 = df1.Year == df1.Year.shift(-1)
c2 = df1.Week == df1.Week.shift(-1)
df1.loc[:,'is_repeated'] = np.where(c1&c2, 'Yes', 'No')

   Year  Week  Value    is_repeated
0  2019     1     20          No
1  2019     2     40          No
2  2019     4     60          No
3  2019     6     75          No
4  2019     7     90          No

Upvotes: 3

roganjosh
roganjosh

Reputation: 13175

The actual comparison check is not incorrect but doesn't work with regular Python if because Pandas works in a vectorized manner. As I said in the comments in regards to the error:

What should Python say to if [1, 2, 3] and [2, 3, 4]:? That'd be True but it wouldn't tell you anything about individual rows (values in the list)

Instead, use np.where.

df1['comparison'] = np.where((df1['Year'] == df1['Year'].shift(-1)) & 
                             (df1['Week'] == df1['Week'].shift(-1)), 'Yes', 'No')

Upvotes: 3

Koralp Catalsakal
Koralp Catalsakal

Reputation: 1124

Well I am not very sure about the bitwise operator, but to compare arrays you can use the equals method and logical and together, I think that would be easier.

For example, you can modify the loop condition to:

if df1['Year'].equals(df1['Year'].shift(-1)) and df1['Week'].equals(df1['Week'].shift(-1)):

Upvotes: 0

Related Questions