这是我的数据框,我需要根据行值(上午,下午,晚上,晚上)的时间小时创建一个新列
这是我的代码
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跟踪(最近的呼叫 最后) ----> 1,如果(prods ['hour']> 4和prods ['hour'] <8): 2个prods ['session'] ='Early Morning' 3个elif(prods ['hour']> 8和prods ['hour'] <12): 4个prods ['session'] ='Morning' 5个Elif(prods ['hour']> 12和prods ['hour'] <16):
/anaconda3/lib/python3.7/site-packages/pandas/core/generic.py在 非零(自己)1476提高ValueError(“ {0}的真值不明确。” 1477“使用a.empty, a.bool(),a.item(),a.any()或a.all()。” -> 1478 .format(self。 class 。 name ))1479 1480 bool = nonzero
ValueError:系列的真值不明确。使用空 a.bool(),a.item(),a.any()或a.all()。
请帮助
答案 0 :(得分:3)
将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
答案 1 :(得分:3)
经过研究,这是我能找到的最简单,最有效的实现方式。
prods['period'] = (prods['hour_int'].dt.hour % 24 + 4) // 4
prods['period'].replace({1: 'Late Night',
2: 'Early Morning',
3: 'Morning',
4: 'Noon',
5: 'Evening',
6: 'Night'}, inplace=True)
我希望这会有所帮助。