【问题标题】:Create multiple columns in pandas aggregation function在 pandas 聚合函数中创建多个列
【发布时间】:2013-01-31 13:40:18
【问题描述】:

我想创建多个列,同时像内置的 ohlc 方法一样重新采样 pandas DataFrame。

def mhl(data):
    return pandas.Series([np.mean(data),np.max(data),np.min(data)],index = ['mean','high','low'])

ts.resample('30Min',how=mhl)

死于

Exception: Must produce aggregated value

有什么建议吗?谢谢!

【问题讨论】:

    标签: python pandas time-series


    【解决方案1】:

    您可以将函数字典传递给resample 方法:

    In [35]: ts
    Out[35]:
    2013-01-01 00:00:00     0
    2013-01-01 00:15:00     1
    2013-01-01 00:30:00     2
    2013-01-01 00:45:00     3
    2013-01-01 01:00:00     4
    2013-01-01 01:15:00     5
    ...
    2013-01-01 23:00:00    92
    2013-01-01 23:15:00    93
    2013-01-01 23:30:00    94
    2013-01-01 23:45:00    95
    2013-01-02 00:00:00    96
    Freq: 15T, Length: 97
    

    创建一个函数字典:

    mhl = {'m':np.mean, 'h':np.max, 'l':np.min}
    

    将字典传递给resamplehow参数:

    In [36]: ts.resample("30Min", how=mhl)
    Out[36]:
                          h     m   l
    2013-01-01 00:00:00   1   0.5   0
    2013-01-01 00:30:00   3   2.5   2
    2013-01-01 01:00:00   5   4.5   4
    2013-01-01 01:30:00   7   6.5   6
    2013-01-01 02:00:00   9   8.5   8
    2013-01-01 02:30:00  11  10.5  10
    2013-01-01 03:00:00  13  12.5  12
    2013-01-01 03:30:00  15  14.5  14
    

    【讨论】:

    • 使用"mean" 比使用np.mean 快大约10 倍。 'min' and 'max' 也是如此
    • 有没有办法为大多数列指定默认值(例如,sum 而不是mean),然后覆盖单个列的方法?
    • 巧妙的技巧:您甚至可以传递函数字典的字典(用于列),如下所示:mhl = {'data_column_1': {'resultA': np.mean, 'resultB': max}, 'data_column_2': {'resultC': min, 'resultD': sum}}
    猜你喜欢
    • 2018-08-21
    • 2017-02-27
    • 1970-01-01
    • 2012-06-12
    • 1970-01-01
    • 2016-01-07
    • 2017-01-17
    • 1970-01-01
    相关资源
    最近更新 更多