在示例数据帧上
data = pd.DataFrame(np.random.rand(6,2), columns = list('ab'))
dti = pd.date_range(start='2019-02-12', end='2019-02-12', periods=6)
data.set_index(dti, inplace=True)
产量:
a b
2019-02-12 00:00:00 0.909822 0.548713
2019-02-12 01:00:00 0.295730 0.452881
2019-02-12 02:00:00 0.889976 0.042893
2019-02-12 03:00:00 0.466465 0.971178
2019-02-12 04:00:00 0.532618 0.769210
2019-02-12 05:00:00 0.947362 0.021689
现在,如何在两列中混合使用分组和合并功能?
假设我有bins = [0, 0.2, 0.4, 0.6, 0.8, 1]
,如何将data
列在a
列上,并在mean
列上获取b
(或最大值,最小值,总和等)每天,每周,每月每个垃圾箱?
答案 0 :(得分:1)
将cut
与DatetimeIndex.day
或DatetimeIndex.week
,DatetimeIndex.month
一起使用,并汇总min
或max
,mean
,{{ 1}}:
sum
还可以通过DataFrameGroupBy.agg
传递多个功能
bins = [0.0, 0.2, 0.4, 0.6, 0.8, 1.0]
labels = ['{}-{}'.format(i + 1, j) for i, j in zip(bins[:-1], bins[1:])]
s = pd.cut(data['a'], bins=bins, labels=labels)
df = data.groupby([data.index.day.rename('day'), s])['b'].min().reset_index()
#df = data.groupby([data.index.week.rename('week'), s])['b'].min().reset_index()
#df = data.groupby([data.index.month.rename('month'), s])['b'].min().reset_index()
print (df)
day a b
0 12 1.4-0.6 0.267070
1 12 1.6-0.8 0.637877
2 12 1.8-1.0 0.299172
df2 = (data.groupby([data.index.day.rename('day'), s])['b']
.agg(['min','max','sum','mean'])
.reset_index())
print (df2)
day a min max sum mean
0 12 1.4-0.6 0.267070 0.267070 0.267070 0.267070
1 12 1.6-0.8 0.637877 0.903206 1.541084 0.770542
2 12 1.8-1.0 0.299172 0.405750 1.098002 0.366001