Python pandas 比较会引发 TypeError:无法将 dtyped [float64] 数组与 [bool] 类型的标量进行比较

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20333435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:13:51  来源:igfitidea点击:

pandas comparison raises TypeError: cannot compare a dtyped [float64] array with a scalar of type [bool]

pythonpandastypeerrordataframe

提问by anonuser0428

I have the following structure to my dataFrame:

我的数据帧具有以下结构:

Index: 1008 entries, Trial1.0 to Trial3.84
Data columns (total 5 columns):
CHUNK_NAME                    1008  non-null values
LAMBDA                        1008  non-null values
BETA                          1008  non-null values
HIT_RATE                      1008  non-null values
AVERAGE_RECIPROCAL_HITRATE    1008  non-null values

chunks=['300_321','322_343','344_365','366_387','388_408','366_408','344_408','322_408','300_408']
lam_beta=[(lambda1,beta1),(lambda1,beta2),(lambda1,beta3),...(lambda1,beta_n),(lambda2,beta1),(lambda2,beta2)...(lambda2,beta_n),........]

my_df.ix[my_df.CHUNK_NAME==chunks[0]&my_df.LAMBDA==lam_beta[0][0]]

I want to get the rows of the Dataframe for a particular chunk lets say chunks[0] and particular lambda value. So in this case the output should be all rows in the dataframe having CHUNK_NAME='300_321' and LAMBDA=lambda1. There would be n rows one for each beta value that would be returned. But instead I get the follwoing error. Any help in solving this problem would be appreciated.

我想获取特定块的 Dataframe 行,比如 chunks[0] 和特定的 lambda 值。所以在这种情况下,输出应该是数据帧中具有 CHUNK_NAME='300_321' 和 LAMBDA=lambda1 的所有行。对于将返回的每个 beta 值,将有 n 行。但相反,我得到了以下错误。任何解决此问题的帮助将不胜感激。

TypeError: cannot compare a dtyped [float64] array with a scalar of type [bool]

采纳答案by ecatmur

&has higher precedence than ==. Write:

&具有比 更高的优先级==。写:

my_df.ix[(my_df.CHUNK_NAME==chunks[0])&(my_df.LAMBDA==lam_beta[0][0])]
         ^                           ^ ^                            ^