[英]TypeError: unsupported operand type(s) for &: 'tuple' and 'tuple'
I'm trying to do some data validation with numpy and I don't really understand this error.我正在尝试使用 numpy 进行一些数据验证,但我不太了解此错误。 see code below:看下面的代码:
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)
Your &
operator is not the one you wanted.您的&
运营商不是您想要的。 &
in python is a bitwise AND, not the logical AND, which is and
. python中的&
是按位与,不是逻辑与,是and
。
Change your code to something like将您的代码更改为类似
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')))
]
for more info, a discussion of the distinction between these different ANDs有关更多信息, 请讨论这些不同的 AND 之间的区别
This has more of a chance of working.这有更多的工作机会。 You haven't provided a sample df
, so I can't test it.你没有提供样本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'))
]
Look at what np.select
expects as its first argument:看看np.select
期望的第一个参数是什么:
condlist : list of bool ndarrays
Each of those lines needs to a bool numpy array.这些行中的每一行都需要一个 bool numpy 数组。
When developing, and debugging, code, test each step.在开发、调试、编码时,测试每一步。 Don't guess or assume.不要猜测或假设。 Look at what np.where(...)
produces.看看np.where(...)
产生了什么。 Does that look at all like a bool array?这看起来像一个 bool 数组吗? And read the docs - for np.where
and np.select
for a start.并阅读文档 - 开始使用np.where
和np.select
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.