熊猫数据框重采样聚合函数使用具有自定义函数的多列?

Sta*_*ish 5 python dataframe pandas

这是一个例子:

# Generate some random time series dataframe with 'price' and 'volume'
x = pd.date_range('2017-01-01', periods=100, freq='1min')
df_x = pd.DataFrame({'price': np.random.randint(50, 100, size=x.shape), 'vol': np.random.randint(1000, 2000, size=x.shape)}, index=x)
df_x.head(10)
                     price   vol
2017-01-01 00:00:00     56  1544
2017-01-01 00:01:00     70  1680
2017-01-01 00:02:00     92  1853
2017-01-01 00:03:00     94  1039
2017-01-01 00:04:00     81  1180
2017-01-01 00:05:00     70  1443
2017-01-01 00:06:00     56  1621
2017-01-01 00:07:00     68  1093
2017-01-01 00:08:00     59  1684
2017-01-01 00:09:00     86  1591

# Here is some example aggregate function:
df_x.resample('5Min').agg({'price': 'mean', 'vol': 'sum'}).head()
                     price   vol
2017-01-01 00:00:00   78.6  7296
2017-01-01 00:05:00   67.8  7432
2017-01-01 00:10:00   76.0  9017
2017-01-01 00:15:00   74.0  6989
2017-01-01 00:20:00   64.4  8078
Run Code Online (Sandbox Code Playgroud)

但是,如果我要提取其他汇总信息取决于多个列,该怎么办?

例如,我想在这里追加2列,分别称为all_upall_down

这两个列的计算定义如下:

在每5分钟内,1分钟采样价格下降了多少倍,成交量下降all_down了多少倍,称为此列,而上涨了多少倍,则称为此列all_up

我期望这2列如下所示:

                     price   vol  all_up  all_down
2017-01-01 00:00:00   78.6  7296       2         0
2017-01-01 00:05:00   67.8  7432       0         0
2017-01-01 00:10:00   76.0  9017       1         0
2017-01-01 00:15:00   74.0  6989       1         1
2017-01-01 00:20:00   64.4  8078       0         2
Run Code Online (Sandbox Code Playgroud)

此功能取决于2列。但是在对象中的agg函数中Resampler,似乎它仅接受3种函数:

  • 一个str或分别应用于每个列的函数。
  • 分别list应用于每个列的函数。
  • dict带键的a 与列名匹配。每次仍仅将函数值应用于单个列。

所有这些功能似乎都不符合我的需求。

jez*_*ael 6

我认为你需要,而不是resample使用groupby+ Grouperapply自定义功能:

def func(x):
   #code
   a = x['price'].mean()
   #custom function working with 2 columns
   b = (x['price'] / x['vol']).mean()
   return pd.Series([a,b], index=['col1','col2'])

df_x.groupby(pd.Grouper(freq='5Min')).apply(func)
Run Code Online (Sandbox Code Playgroud)

resample用于所有受支持的聚集函数并将输出与自定义函数的输出连接在一起:

def func(x):
    #custom function
    b = (x['price'] / x['vol']).mean()
    return b

df1 = df_x.groupby(pd.Grouper(freq='5Min')).apply(func)
df2 = df_x.resample('5Min').agg({'price': 'mean', 'vol': 'sum'}).head()

df = pd.concat([df1, df2], axis=1)
Run Code Online (Sandbox Code Playgroud)

编辑:对于检查减少和增加使用功能diff并与比较0,将条件与&和计数为sum

def func(x):
    v = x['vol'].diff().fillna(0)
    p = x['price'].diff().fillna(0)
    m1 = (v > 0) & (p > 0)
    m2 = (v < 0) & (p < 0) 
    return pd.Series([m1.sum(), m2.sum()], index=['all_up','all_down'])


df1 = df_x.groupby(pd.Grouper(freq='5min')).apply(func)
print (df1)
                     all_up  all_down
2017-01-01 00:00:00       2         0
2017-01-01 00:05:00       0         0

df2 = df_x.resample('5Min').agg({'price': 'mean', 'vol': 'sum'}).head()
df = pd.concat([df2, df1], axis=1)
print (df)
                      vol  price  all_up  all_down
2017-01-01 00:00:00  7296   78.6       2         0
2017-01-01 00:05:00  7432   67.8       0         0
Run Code Online (Sandbox Code Playgroud)