我有一个包含两列的DataFrame
userID duration
0 DSm7ysk 03:08:49
1 no51CdJ 00:35:50
2 ...
'持续时间'有类型timedelta。我尝试过使用
bins = [dt.timedelta(minutes = 0), dt.timedelta(minutes =
5),dt.timedelta(minutes = 10),dt.timedelta(minutes =
20),dt.timedelta(minutes = 30), dt.timedelta(hours = 4)]
labels = ['0-5min','5-10min','10-20min','20-30min','30min+']
df['bins'] = pd.cut(df['duration'], bins, labels = labels)
但是,分箱数据不会使用指定的分箱,而是在帧中的每个持续时间内创建。
将timedelta对象分成不规则区间的最简单方法是什么?或者我只是错过了一些明显的东西?
答案 0 :(得分:0)
它对我来说适用于熊猫0.23.4
import pandas as pd
import numpy as np
df = pd.DataFrame({
'userID': ['DSm7ysk', 'no51CdJ', 'foo', 'bar'],
'duration': [pd.Timedelta('3 hours 8 minutes 49 seconds'), pd.Timedelta('35 minutes 50 seconds'), pd.Timedelta('1 minutes 13 seconds'), pd.Timedelta('6 minutes 43 seconds')]
})
bins = [
pd.Timedelta(minutes = 0),
pd.Timedelta(minutes = 5),
pd.Timedelta(minutes = 10),
pd.Timedelta(minutes = 20),
pd.Timedelta(minutes = 30),
pd.Timedelta(hours = 4)
]
labels = ['0-5min', '5-10min', '10-20min', '20-30min', '30min+']
df['bins'] = pd.cut(df['duration'], bins, labels = labels)
结果:
答案 1 :(得分:0)
您可以归一化为装仓前的秒。这样可以减少对整数进行装箱的问题。
df = pd.DataFrame({'userID': ['A', 'B'],
'duration': pd.to_timedelta(['00:08:49', '00:35:50'])})
L = ['00:00:00', '00:05:00', '00:10:00', '00:20:00', '00:30:00', '04:00:00']
bins = pd.to_timedelta(L).total_seconds()
cats = ['0-5min', '5-10min', '10-20min', '20-30min', '30min+']
df['bins'] = pd.cut(df['duration'].dt.total_seconds(), bins, labels=cats)
print(df)
# duration userID bins
# 0 00:08:49 A 5-10min
# 1 00:35:50 B 30min+