这是我的数据框,我需要根据时间值创建一个新列,行值是(早上,下午,晚上,晚上)
这是我的代码
if ((prods['hour'] < 4) & (prods['hour'] > 8 )):
prods['session'] = 'Early Morning'
elif ((prods['hour'] < 8) & (prods['hour'] > 12 )):
prods['session'] = 'Morning'
elif ((prods['hour'] < 12) & (prods['hour'] > 16 )):
prods['session'] = 'Noon'
elif ((prods['hour'] < 16) & (prods['hour'] > 20 )):
prods['session'] = 'Eve'
elif ((prods['hour'] < 20) & (prods['hour'] > 24 )):
prods['session'] = 'Night'
elif ((prods['hour'] < 24) & (prods['hour'] > 4 )):
prods['session'] = 'Late Night'
这是我得到的错误
ValueError Traceback(最近一次调用最后一次)在----> 1 if(prods ['hour']> 4并且prods ['hour'] <8):2 prods ['session'] ='Early Morning'3 elif (prods ['hour']> 8 and prods ['hour'] <12):4 prods ['session'] ='Morning'5 elif(prods ['hour']> 12 and prods ['hour'] < 16):
/anaconda3/lib/python3.7/site-packages/pandas/core/generic.py in nonzero(self)1476引发ValueError(“{0}的真值是不明确的。”1477“使用a.empty,a .bool(),a.item(),a.any()或a.all()。“ - > 1478 .format(self.class.name))1479 1480 bool =非零
ValueError:Series的真值是不明确的。使用a.empty,a.bool(),a.item(),a.any()或a.all()。
请帮助
使用cut
或自定义函数与and
,并将<
更改为>
和>
到<=
以及每个值添加return
:
prods = pd.DataFrame({'hour':range(1, 25)})
b = [0,4,8,12,16,20,24]
l = ['Late Night', 'Early Morning','Morning','Noon','Eve','Night']
prods['session'] = pd.cut(prods['hour'], bins=b, labels=l)
def f(x):
if (x > 4) and (x <= 8):
return 'Early Morning'
elif (x > 8) and (x <= 12 ):
return 'Morning'
elif (x > 12) and (x <= 16):
return'Noon'
elif (x > 16) and (x <= 20) :
return 'Eve'
elif (x > 20) and (x <= 24):
return'Night'
elif (x <= 4):
return'Late Night'
prods['session1'] = prods['hour'].apply(f)
print (prods)
hour session session1
0 1 Late Night Late Night
1 2 Late Night Late Night
2 3 Late Night Late Night
3 4 Late Night Late Night
4 5 Early Morning Early Morning
5 6 Early Morning Early Morning
6 7 Early Morning Early Morning
7 8 Early Morning Early Morning
8 9 Morning Morning
9 10 Morning Morning
10 11 Morning Morning
11 12 Morning Morning
12 13 Noon Noon
13 14 Noon Noon
14 15 Noon Noon
15 16 Noon Noon
16 17 Eve Eve
17 18 Eve Eve
18 19 Eve Eve
19 20 Eve Eve
20 21 Night Night
21 22 Night Night
22 23 Night Night
23 24 Night Night