2017-02-28 44 views
0

在閱讀了關於該主題的其他一些Stackoverflow文章後,我想出了下面的代碼,但我不斷收到錯誤。如何有條件地設置熊貓列?

df6['Status'] in ['Admitted' , 'Admitted from WL', 'Matriculating'] 

這並沒有工作,要麼

我不斷收到以下錯誤:

Traceback (most recent call last): 
File "<pyshell#52>", line 1, in <module> 
exec(open("C:\\python\\xxxxxx\\Analysis\\clean_data_v6.py").read()) 
File "<string>", line 110, in <module> 
File "C:\Users\xxxxxxx\AppData\Local\Programs\Python\Python35-32\lib\site 
packages\pandas\core\generic.py", line 917, in __nonzero__ 
.format(self.__class__.__name__)) 
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), 
a.item(), a.any() or a.all(). 

df6['Accepted'] = np.where((df6['Status'] == 'Admitted' or df6['Status'] == 
'Admitted from WL' or df6['Status'] == 'Matriculating'), '1', '0') 

我也使用以下,而不是 「ORS」 嘗試我需要改變什麼?

回答

4

取而代之的是:

(df6['Status'] == 'Admitted' or df6['Status'] == 'Admitted from WL' or df6['Status'] == 'Matriculating') 

你會需要這樣的:

(df6['Status'] == 'Admitted') | (df6['Status'] == 'Admitted from WL') | (df6['Status'] == 'Matriculating') 

或更容易:

df6.Status.isin(['Admitted' , 'Admitted from WL', 'Matriculating']) 
+0

謝謝!這工作。 –