简体   繁体   English

Python-pandas:一个系列的真值是模棱两可的错误

[英]Python-pandas: the truth value of a series is ambiguous error

in pandas_dataframe, I try to convert numerical value to categorical value在 pandas_dataframe 中,我尝试将数值转换为分类值

df['SalePrice_band']=0
df.loc[df['SalePrice']<50000 , 'SalesPrice_band']=1
df.loc[df['SalePrice']>=50000 & df['SalePrice']<100000 , 'SalesPrice_band'] = 2
df.loc[df['SalePrice']>=100000 & df['SalePrice']<125000 , 'SalesPrice_band'] = 3
df.loc[df['SalePrice']>=125000 & df['SalePrice']<150000 , 'SalesPrice_band'] = 4
df.loc[df['SalePrice']>=150000 & df['SalePrice']<175000 , 'SalesPrice_band'] = 5

But, above code occurs error as: ValueError: The truth value of a Series is ambiguous.但是,上面的代码出现错误为:ValueError:一个系列的真值是不明确的。 Use a.empty, a.bool(), a.item(), a.any() or a.all().使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。

so, I read error message and checked所以,我阅读了错误消息并检查了

df.loc[df['SalePrice']<50000 , 'SalesPrice_band']=1

Above is fine.上面还好。

 df['SalePrice']>=50000 & df['SalePrice']<100000

But Here, I got error where I connect by two bool_bin但是在这里,我通过两个 bool_bin 连接时遇到错误

SO, I TRY Like this:所以,我尝试这样:

(df['SalePrice']>=50000 & df['SalePrice']<100000).all()

But DOESN'T WORK;但不起作用; Still error: ValueError: The truth value of a Series is ambiguous.仍然错误: ValueError: Series 的真值不明确。 Use a.empty, a.bool(), a.item(), a.any() or a.all().使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。

How can I handle it?我该如何处理?

In Pandas, you must put all boolean checks inside parenthesis, like below:在 Pandas 中,您必须将所有 boolean 检查放在括号内,如下所示:

df.loc[(df['SalePrice']>=50000) & (df['SalePrice']<100000) , 'SalesPrice_band'] = 2

instead of this:而不是这个:

df.loc[df['SalePrice']>=50000 & df['SalePrice']<100000 , 'SalesPrice_band'] = 2

The latest will lead to the error that you provided最新的将导致您提供的错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM