pandas Python 错误:TypeError:无法将 dtyped [float64] 数组与 [bool] 类型的标量进行比较
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38252423/
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
Python error: TypeError: cannot compare a dtyped [float64] array with a scalar of type [bool]
提问by mysterious_guy
Below is a small sample of my dataframe.
下面是我的数据框的一个小样本。
In [10]: df
Out[10]:
TXN_KEY Send_Agent Pay_Agent Send_Amount Pay_Amount
0 13272184 AWD120279 AEU002152 85.99 85.04
1 13272947 ARA030122 AEV000034 402.01 400.00
2 13273453 API185805 API190139 22.98 22.98
3 13273870 ANO080012 API352676 490.00 475.68
4 13274676 AUK359401 AED002782 616.16 600.87
5 13274871 ACL000105 ACB020203 193.78 185.21
6 13275194 ADA374245 ANP023424 100.00 98.50
grouped = df.groupby('Send_Agent')
A function to calculate the number of times the Send_Amount is same as the next one works just fine.
计算 Send_Amount 与下一个相同次数的函数工作得很好。
def repeat_count(s):
return (s.shift() == s).sum()
In [35]: result = grouped.agg({'Send_Amount':[repeat_count]})
In [36]: result
Out[36]:
Send_Amount
repeat_count
Send_Agent
A03010016 0
A03020026 0
A10000075 0
A10000316 0
A10000932 0
A10060013 0
A10080022 0
A11120030 0
But when I am trying to calculate the count of all the Send_Agent where Send_Amount is between 900 and 1000 and I am getting an error:
但是,当我尝试计算 Send_Amount 介于 900 和 1000 之间的所有 Send_Agent 的计数时,出现错误:
In [27]: def banded_txns(s):
return(s >=900.00 & s < 1000.00).count()
In [28]: result = grouped.agg({'Send_Amount':banded_txns})
TypeError: cannot compare a dtyped [float64] array with a scalar of type [bool]
I am not able to figure out why it is working for one type of comparison and not for the other. Thanks in advance.
我无法弄清楚为什么它适用于一种比较而不适用于另一种比较。提前致谢。
回答by Alberto Garcia-Raboso
You're missing some parentheses:
您缺少一些括号:
def banded_txns(s):
return ((s >= 900.00) & (s < 1000.00)).count()