[英]TypeError: unsupported operand type(s) for /: 'tuple' and 'int'
[英]TypeError: unsupported operand type(s) for &: 'tuple' and 'tuple'
我正在尝试使用 numpy 进行一些数据验证,但我不太了解此错误。 看下面的代码:
conditions = [
(np.where(df['DD'].eq('No DD Required'))),
(np.where(df['DD'].eq('DD Required'))) & (np.where(df['On Direct Debit'].eq('No'))),
(np.where(df['DD'].eq('DD Required'))) & (np.where(df['On Direct Debit'].eq('Yes')))
]
values = ['Pass', 'Fail', 'Pass']
df['DDValidation'] = np.select(conditions, values)
您的&
运营商不是您想要的。 python中的&
是按位与,不是逻辑与,是and
。
将您的代码更改为类似
conditions = [
(np.where(df['DD'].eq('No DD Required'))),
(np.where(df['DD'].eq('DD Required'))) and (np.where(df['On Direct Debit'].eq('No'))),
(np.where(df['DD'].eq('DD Required'))) and (np.where(df['On Direct Debit'].eq('Yes')))
]
有关更多信息, 请讨论这些不同的 AND 之间的区别
这有更多的工作机会。 你没有提供样本df
,所以我无法测试它。
conditions = [
df['DD'].eq('No DD Required',
(df['DD'].eq('DD Required')) & (df['On Direct Debit'].eq('No')),
(df['DD'].eq('DD Required')) & (df['On Direct Debit'].eq('Yes'))
]
看看np.select
期望的第一个参数是什么:
condlist : list of bool ndarrays
这些行中的每一行都需要一个 bool numpy 数组。
在开发、调试、编码时,测试每一步。 不要猜测或假设。 看看np.where(...)
产生了什么。 这看起来像一个 bool 数组吗? 并阅读文档 - 开始使用np.where
和np.select
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.