使用* multiple *列上的* multiple * * named * aggregation函数重新采样和聚合

时间:2017-10-02 14:05:29

标签: python pandas

我有一个像

这样的数据框
import pandas as pd
import numpy as np
range = pd.date_range('2015-01-01', '2015-01-5', freq='15min')
df = pd.DataFrame(index = range)
df['speed'] = np.random.randint(low=0, high=60, size=len(df.index))
df['otherF'] = np.random.randint(low=2, high=42, size=len(df.index))

我可以轻松地重新取样并将内置应用为 sum()

df['speed'].resample('1D').sum()
Out[121]: 
2015-01-01    2865
2015-01-02    2923
2015-01-03    2947
2015-01-04    2751

我还可以应用返回多个值的自定义函数:

def mu_cis(x):
     x_=x[~np.isnan(x)]
     CI=np.std(x_)/np.sqrt(x.shape)
     return np.mean(x_),np.mean(x_)-CI,np.mean(x_)+CI,len(x_)

df['speed'].resample('1D').agg(mu_cis)
Out[122]: 
2015-01-01     (29.84375, [28.1098628611], [31.5776371389], 96)
2015-01-02    (30.4479166667, [28.7806726396], [32.115160693...
2015-01-03    (30.6979166667, [29.0182072972], [32.377626036...
2015-01-04       (28.65625, [26.965228204], [30.347271796], 96)

正如我在这里读到的,我甚至可以使用名称pandas apply function that returns multiple values to rows in pandas dataframe

的多个值
def myfunc1(x):
    x_=x[~np.isnan(x)]
    CI=np.std(x_)/np.sqrt(x.shape)
    e=np.mean(x_) 
    f=np.mean(x_)+CI
    g=np.mean(x_)-CI
    return pd.Series([e,f,g], index=['MU', 'MU+', 'MU-'])

df['speed'].resample('1D').agg(myfunc1)

给出了

Out[124]: 
2015-01-01  MU             29.8438
        MU+    [31.5776371389]
        MU-    [28.1098628611]
2015-01-02  MU             30.4479
        MU+    [32.1151606937]
        MU-    [28.7806726396]
2015-01-03  MU             30.6979
        MU+    [32.3776260361]
        MU-    [29.0182072972]
2015-01-04  MU             28.6562
        MU+     [30.347271796]
        MU-     [26.965228204]

但是,当我尝试将其应用于所有原始列时,我只得到NaN s:

df.resample('1D').agg(myfunc1)
Out[127]: 
        speed  otherF
2015-01-01    NaN     NaN
2015-01-02    NaN     NaN
2015-01-03    NaN     NaN
2015-01-04    NaN     NaN
2015-01-05    NaN     NaN

agg后使用applyresample()更改结果。

这样做的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

问题出在myfunc1。它会尝试返回pd.Series,而您有pd.DataFrame。以下似乎工作正常。

def myfunc1(x):
    x_=x[~np.isnan(x)]
    CI=np.std(x_)/np.sqrt(x.shape)
    e=np.mean(x_)
    f=np.mean(x_)+CI
    g=np.mean(x_)-CI
    try:
        return pd.DataFrame([e,f,g], index=['MU', 'MU+', 'MU-'], columns = x.columns)
    except AttributeError: #will still raise errors of other nature
        return pd.Series([e,f,g], index=['MU', 'MU+', 'MU-'])

可替换地:

def myfunc1(x):
    x_=x[~np.isnan(x)]
    CI=np.std(x_)/np.sqrt(x.shape)
    e=np.mean(x_)
    f=np.mean(x_)+CI
    g=np.mean(x_)-CI
    if x.ndim > 1: #Equivalent to if len(x.shape) > 1
        return pd.DataFrame([e,f,g], index=['MU', 'MU+', 'MU-'], columns = x.columns)
    return pd.Series([e,f,g], index=['MU', 'MU+', 'MU-'])