简体   繁体   English

TypeError: &: 'tuple' 和 'tuple' 不支持的操作数类型

[英]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.wherenp.select

暂无
暂无

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

相关问题 类型错误:不支持 / 的操作数类型:'tuple' 和 'int' - TypeError: unsupported operand type(s) for /: 'tuple' and 'int' 类型错误:% 不支持的操作数类型:'tuple' 和 'int' - TypeError: unsupported operand type(s) for %: 'tuple' and 'int' TypeError:|:'tuple'和'bool'不支持的操作数类型 - TypeError: unsupported operand type(s) for |: 'tuple' and 'bool' TypeError:+的不支持的操作数类型:'float'和'tuple' - TypeError: unsupported operand type(s) for +: 'float' and 'tuple' 类型错误:% 不支持的操作数类型:'tuple' 和 'str' - TypeError: unsupported operand type(s) for %: 'tuple' and 'str' TypeError:+不支持的操作数类型:“ int”和“ tuple” - TypeError: unsupported operand type(s) for +: 'int' and 'tuple' scipy 最小化 TypeError: 不支持的操作数类型 -: 'tuple' 和 'tuple' - scipy minimize TypeError: unsupported operand type(s) for -: 'tuple' and 'tuple' Python写错误-TypeError:%不支持的操作数类型:'tuple'和'tuple' - Python write error - TypeError: unsupported operand type(s) for %: 'tuple' and 'tuple' Python — TypeError:%不支持的操作数类型:“ NoneType”和“ tuple” - Python — TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple' Python 2.7 TypeError:%不支持的操作数类型:'NoneType'和'tuple - Python 2.7 TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM