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