Skip to content Skip to sidebar Skip to footer

How Do I Building Dt.hour In 2 Days

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

Solution 1:

Use GroupBy.transform with factorize for count dates and last join all together:

a=df.groupby('customer_id')['timestamp'].transform(lambdax:pd.factorize(x.dt.date)[0])+1df['hour']='Day'+a.astype(str)+' - '+df['timestamp'].dt.hour.astype(str)print(df)customer_idvaluetimestamphour011000 2018-05-28 03:40:00  Day1-3111450 2018-05-28 04:40:01  Day1-4211040 2018-05-28 05:40:00  Day1-5311500 2018-05-29 03:40:00  Day2-3411090 2018-05-29 04:40:00  Day2-4531060 2018-05-18 03:40:00  Day1-3631040 2018-05-18 05:40:00  Day1-5731520 2018-05-19 03:40:00  Day2-3831490 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

Post a Comment for "How Do I Building Dt.hour In 2 Days"