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

data=pd.read_csv(‘附件1:sale_info.csv’,parse_dates=[‘date_rcd’])
data[‘year’]=data.date_rcd.dt.year
data[‘month’]=data.date_rcd.dt.month
data[‘day’]=data.date_rcd.dt.day
data=pd.DataFrame(data)
data=data[data[‘year’]==2018]
data=data[((data[‘month’] <10.0)&(data[‘month’] >= 7.0))or((data[‘month’]==10.0)&(data[‘day’]==1.0))]
data.eval(‘profit=s*real_cost’,inplace=True)
data=data.groupby(‘skc’)[‘profit’].sum()
data=data.sort_values(ascending=False)
print(data)
data.to_csv(“Test_1.csv”, encoding=“utf-8-sig”, header=True, index=True)

Use | for or and & for and.

Pandas follows the numpy convention of raising an error when you try to convert something to a bool. This happens in a if or when using the boolean operations, and, or, or not. It is not clear what the result of.

example

5 == pd.Series([12,2,5,10])

The result you get is a Series of booleans, equal in size to the pd.Series in the right hand side of the expression. So, you get an error. The problem here is that you are comparing a pd.Series with a value, so you’ll have multiple True and multiple False values, as in the case above. This of course is ambiguous, since the condition is neither True or False. You need to further aggregate the result so that a single boolean value results from the operation. For that you’ll have to use either any or all depending on whether you want at least one (any) or all values to satisfy the condition.

(5 == pd.Series([12,2,5,10])).all()
# False

or

(5 == pd.Series([12,2,5,10])).any()
# True