如何在2天内建立dt.hour [英] How do I building dt.hour in 2 days
本文介绍了如何在2天内建立dt.hour的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我做了多天观察,几天后可以观察到一位客户,这是我的数据
I did multi-day observation, one customer can be observed in more few days, Here's my data
customer_id value timestamp
1 1000 2018-05-28 03:40:00.000
1 1450 2018-05-28 04:40:01.000
1 1040 2018-05-28 05:40:00.000
1 1500 2018-05-29 03:40:00.000
1 1090 2018-05-29 04:40:00.000
3 1060 2018-05-18 03:40:00.000
3 1040 2018-05-18 05:40:00.000
3 1520 2018-05-19 03:40:00.000
3 1490 2018-05-19 04:40:00.000
我所做的是 df ['hour'] = df ['timestamp']。dt.hour
,但是它只显示小时,但是为什么我需要呢,因为实验周期可以是2-6天
What I did is df['hour'] = df['timestamp'].dt.hour
, but it only show the hour, but why I need is, because experiment cycle is can be 2-6 days
customer_id value timestamp hour
1 1000 2018-05-28 03:40:00.000 Day1 - 3
1 1450 2018-05-28 04:40:01.000 Day1 - 4
1 1040 2018-05-28 05:40:00.000 Day1 - 5
1 1500 2018-05-29 03:40:00.000 Day1 - 3
1 1090 2018-05-29 04:40:00.000 Day2 - 4
3 1060 2018-05-18 03:40:00.000 Day1 - 3
3 1040 2018-05-18 05:40:00.000 Day1 - 5
3 1520 2018-05-19 03:40:00.000 Day2 - 3
3 1490 2018-05-19 04:40:00.000 Day2 - 4
推荐答案
使用 GroupBy.transform
与 factorize
的计数为 date
s和最后加入所有一起er:
Use GroupBy.transform
with factorize
for count date
s and last join all together:
a = df.groupby('customer_id')['timestamp'].transform(lambda x: pd.factorize(x.dt.date)[0]) + 1
df['hour'] = 'Day' + a.astype(str) + ' - ' + df['timestamp'].dt.hour.astype(str)
print (df)
customer_id value timestamp hour
0 1 1000 2018-05-28 03:40:00 Day1 - 3
1 1 1450 2018-05-28 04:40:01 Day1 - 4
2 1 1040 2018-05-28 05:40:00 Day1 - 5
3 1 1500 2018-05-29 03:40:00 Day2 - 3
4 1 1090 2018-05-29 04:40:00 Day2 - 4
5 3 1060 2018-05-18 03:40:00 Day1 - 3
6 3 1040 2018-05-18 05:40:00 Day1 - 5
7 3 1520 2018-05-19 03:40:00 Day2 - 3
8 3 1490 2018-05-19 04:40:00 Day2 - 4
每组连续日期的替代解决方案:
Alternative solution if consecutive dates per groups:
dates = df['timestamp'].dt.date
a = dates.sub(dates.groupby(df['customer_id']).transform('min')).dt.days + 1
这篇关于如何在2天内建立dt.hour的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文