gpt4 book ai didi

python - 在 Python 数据框中获取一天的一部分(早上、下午、晚上、晚上)

转载 作者:行者123 更新时间:2023-12-02 03:26:06 26 4
gpt4 key购买 nike

这是我的数据框,我需要根据行值(早上、下午、晚上、晚上)的时间创建一个新列

DataFrame

这是我的代码

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 (most recent call last) in ----> 1 if (prods['hour'] > 4 and 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 raise ValueError("The truth value of a {0} is ambiguous. " 1477 "Use a.empty, a.bool(), a.item(), a.any() or a.all()." -> 1478 .format(self.class.name)) 1479 1480 bool = nonzero

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or 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, include_lowest=True)
<小时/>
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

关于python - 在 Python 数据框中获取一天的一部分(早上、下午、晚上、晚上),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55571311/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com