2015-10-19 78 views
1
A   B   C   D 
0 0.397333 Xor   0.569748 0.406415 
1 0.319684 x   0.159117 0.522648 
2 0.778038 0.486989 x   x 
3 0.549993 0.896913 0.960814 0.430113 
4 0.251655 0.802137 Xand  0.218265 

在這裏,我需要比較所有四列,我需要有一個新的column E我將有我的新信息。如何比較大熊貓列和創建新列是或否

我需要檢查是否有任何四列包含xColumn E將有價值Yes其他No

輸出

A   B   C   D   E 
0 0.397333 Xor   0.569748 0.406415 No 
1 0.319684 x   0.159117 0.522648 Yes 
2 0.778038 0.486989 x   x   Yes 
3 0.549993 0.896913 0.960814 0.430113 No 
4 x   0.802137 Xand  0.218265 Yes 

我想使用這裏那裏條款,但我不能做到這一點,也拉姆達我無法理解我應該怎麼寫。

這裏是我的代碼:

def YorN(stri): 
    if stri =='x': 
     return True 
    else: 
     return False 

df['E'] = np.where(YorN(df.B) | YorN(df.C) | YorN(df.D)| YorN(df.A), 'Yes', 'No') 

錯誤:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). 

編輯1 我的其他列可能包含一些其他變量

回答

4

你比較FUNC將無法正常工作你已經發現,你試圖比較一個標量和一個數組。無論如何,您可以撥打apply並通過axis=1來按行處理df。轉換的D型到str,這樣就可以使用矢量化str.containsany產生boolean值系列,並以此作爲與ARG爲np.where和回報「是」或「否」時分別TrueFalse

In [8]: 
df['E'] = np.where(df.astype(str).apply(lambda x: x.str.contains('x').any(), axis=1), 'yes', 'no') 
df 

Out[8]: 
      A   B   C   D E 
0 0.397333 0.245596 0.569748 0.406415 no 
1 0.319684   x 0.159117 0.522648 yes 
2 0.778038 0.486989   x   x yes 
3 0.549993 0.896913 0.960814 0.430113 no 
4 0.251655 0.802137 0.024341 0.218265 no 

編輯

答案仍然有效:

In [10]: 
df['E'] = np.where(df.astype(str).apply(lambda x: x.str.contains('x').any(), axis=1), 'yes', 'no') 
df 

Out[10]: 
      A   B   C   D E 
0 0.397333  Xor 0.569748 0.406415 no 
1 0.319684   x 0.159117 0.522648 yes 
2 0.778038 0.486989   x   x yes 
3 0.549993 0.896913 0.960814 0.430113 no 
4 0.251655 0.802137  Xand 0.218265 no 
+0

對不起EdChum但如果我有比這n個其他的一些信息棕褐色比我不能使用'.contains'。但是,謝謝回覆 –

+0

除非您發佈有代表性數據和信息的問題,否則它會讓SO用戶浪費時間回答毫無意義的問題,因此您還沒有完全解釋爲什麼我的解決方案無法正常工作 – EdChum

+0

對不起,我更新了我的有點問題。現在,如果我使用包含'x',那麼我認爲它不會工作。 –