如何有条件地设置列?

2024-04-23 07:15:18 发布

您现在位置:Python中文网/ 问答频道 /正文

在阅读了其他一些关于这个主题的Stackoverflow文章之后,我想到了下面的代码,但是我不断地遇到错误。你知道吗

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().

我需要改变什么?你知道吗


Tags: orinfrompy主题status错误line
1条回答
网友
1楼 · 发布于 2024-04-23 07:15:18

而不是这样:

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

相关问题 更多 >