我正在嘗試將 If 陳述句從 excel 轉換為 python。
我知道對于 excel,If 陳述句我們有:
IF(logical_test, [value_if_true], [value_if_false])
對于 Python,我想復制下面的 excel if 陳述句。
AZ2 = IF(BA2<>"",IF(BB2<>"",BB2,"Blank"), IF(BO2<>"", BO2, "Blank"))
這里AZ2,BA2,BB2,BO2是excel檔案中的列,<>在excel中代表不相等。
我嘗試使用 np.where 使用以下代碼,但它不起作用?你知道我做錯了什么嗎?
df['AZ']=np.where(df['BA'].notnull(),
np.where(df['BB'].notnull(),df['BB'],'Blank',
np.where(df['BO'].notnull(),df['BO'],'Blank')))
當我嘗試執行它時出現此錯誤。
Type Error: where() takes at most 3 arguments (4 given)
有什么建議?
uj5u.com熱心網友回復:
問題來自第一個括號后面缺少的括號Blank和陳述句末尾的額外括號。
你可以試試這個:
df['AZ']=np.where(df['BA'].notnull(),
np.where(df['BB'].notnull(),
df['BB'],
'Blank'),
np.where(df['BO'].notnull(),
df['BO'],
'Blank'))
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/312274.html
標籤:熊猫 数据框 python-2.7 麻木的
上一篇:輸入數字時如何顯示有數字的串列
