Python / Pandas Binning数据每小时
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python / Pandas Binning数据每小时相关的知识,希望对你有一定的参考价值。
我有一个包含两列的DataFrame
userID duration
0 DSm7ysk 03:08:49
1 no51CdJ 00:35:50
2 ...
'duration'具有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.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)
结果:
另一答案
您可以在装箱前将其标准化为秒。这减少了对整数进行分箱的问题。
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+
以上是关于Python / Pandas Binning数据每小时的主要内容,如果未能解决你的问题,请参考以下文章
sensor的skipping and binning 模式