我想從我的資料集中的時間列中添加班次名稱。
7:00:00 to 17:00:00 (same day) - First Shift
17:00:01 to 4:00:00 (next morning) - Second Shift
4:00:01 to 6:59:59 (same morning) - Dead Time
我遵循了一些給定的解決方案,形成堆疊溢位,但徒勞無功。資料集一瞥
Date Time
2022-04-13 16:57:00
2022-09-07 10:46:00
df = pd.DataFrame({'Time':range(1, 25)})
b = [4,7,17,24]
l = ['Dead Time', 'First Shift','Second Shift']
df['Shifts'] = pd.cut(df['Time'], bins=b, labels=l, include_lowest=True)
uj5u.com熱心網友回復:
# define bins
b=[-1, 3, 6, 16, 24]
# define labels
l = ['Second Shift','Dead Time', 'First Shift','Second Shift']
# capture the hour from the Time and then label using pd.cut
df['shift']=pd.cut( pd.to_datetime (df['Time']).dt.hour,
bins=b,
labels=l,
ordered=False)
df
Date Time shift
0 2022-04-13 01:57:00 Second Shift
1 2022-09-07 02:46:00 Second Shift
2 2022-04-13 04:57:00 Dead Time
3 2022-04-13 05:57:00 Dead Time
4 2022-04-13 07:57:00 First Shift
5 2022-04-13 10:57:00 First Shift
6 2022-04-13 12:57:00 First Shift
7 2022-04-13 17:57:00 Second Shift
8 2022-04-13 19:57:00 Second Shift
9 2022-04-13 22:57:00 Second Shift
如果您輪班在 00 分鐘的整點結束,則輪班時間 1 分鐘及以上的垃圾箱將起作用,無需查看分鐘
df['shift']=pd.cut( (pd.to_datetime (df['Time']) pd.offsets.Minute(-1)) .dt.hour,
bins=b,
labels=l,
ordered=False)
df
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/525770.html
標籤:熊猫数据框
上一篇:將字串分離到資料框中
下一篇:根據一些相似的列合并兩個資料框
