如何有条件地设置 pandas 列?

How to set a pandas column conditionally?

在阅读了有关该主题的其他几篇 Whosebug 文章后,我想出了以下代码,但我不断收到错误。

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

我也尝试使用以下而不是 "ors":

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['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'])