簡體   English   中英

TypeError: &: 'tuple' 和 'tuple' 不支持的操作數類型

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM