本文介绍了使用if else语句的输出创建新列会导致错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用以下代码,
if(df.month == 3 or df.month == 4 or df.month == 5):
df.test = 'A'
elif(df.month == 6 or df.month == 7 or df.month == 8):
df.test = 'B'
else:
df.test = 'C'
但是使用此方法时,出现以下错误,
But while using this, I am getting the following error,
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
更新:
print df.columns
Unnamed: 0 int64
year int64
month int64
day int64
dep_time float64
dep_delay float64
arr_time float64
arr_delay float64
carrier object
tailnum object
flight int64
origin object
dest object
air_time float64
distance int64
hour float64
minute float64
print df.dtypes
dtype: object
有人可以帮助我在这里找到错误吗?
Can anybody help me in finding the error here?
推荐答案
我认为最好是使用和,因为您无法使用 if
将标量与类似数组的标量进行比较或 elif
变得模棱两可:
I think the best is use loc
and isin
, because you can't compare a scalar with an array like that using if
or elif
it becomes ambiguous:
print df
year month day
0 2005 3 20
1 2005 4 20
2 2005 5 20
3 2005 6 20
4 2005 7 20
5 2005 8 20
6 2005 9 20
df['test'] = 'C'
df.loc[df['month'].isin([3,4,5]) , 'test'] = 'A'
df.loc[df['month'].isin([6,7,8]) , 'test'] = 'B'
print df
year month day test
0 2005 3 20 A
1 2005 4 20 A
2 2005 5 20 A
3 2005 6 20 B
4 2005 7 20 B
5 2005 8 20 B
6 2005 9 20 C
或者您可以通过以下方式用值 C
填充列 test
:
Or you can fill column test
by value C
this way:
df.loc[df['month'].isin([3,4,5]) , 'test'] = 'A'
df.loc[df['month'].isin([6,7,8]) , 'test'] = 'B'
df.loc[df['month'].isin([1,2,9,10,11,12]) , 'test'] = 'C'
print df
year month day test
0 2005 3 20 A
1 2005 4 20 A
2 2005 5 20 A
3 2005 6 20 B
4 2005 7 20 B
5 2005 8 20 B
6 2005 9 20 C
这篇关于使用if else语句的输出创建新列会导致错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!