将pandas.resample()。agg()与'interpolate'一起使用

我需要对df,具有不同功能的不同列进行重新采样。

import pandas as pd
import numpy as np

df=pd.DataFrame(index=pd.DatetimeIndex(start='2020-01-01 00:00:00',end='2020-01-02 00:00:00',freq='3H'),data=np.random.rand(9,3),columns=['A','B','C'])

df = df.resample('1H').agg({'A': 'ffill','B': 'interpolate','C': 'max'})

“平均值”,“最大值”,“求和”之类的功能起作用。

但是似乎不能那样使用'interpolate'。

有什么解决方法吗?

nnaxi 回答:将pandas.resample()。agg()与'interpolate'一起使用

一种解决方法是concat使用不同的聚合帧:

df_out = pd.concat([df[['A']].resample('1H').ffill(),df[['B']].resample('1H').interpolate(),df[['C']].resample('1H').max().ffill()],axis=1)

[出]

                            A         B         C
2020-01-01 00:00:00  0.836547  0.436186  0.520913
2020-01-01 01:00:00  0.836547  0.315646  0.520913
2020-01-01 02:00:00  0.836547  0.195106  0.520913
2020-01-01 03:00:00  0.577291  0.074566  0.754697
2020-01-01 04:00:00  0.577291  0.346092  0.754697
2020-01-01 05:00:00  0.577291  0.617617  0.754697
2020-01-01 06:00:00  0.490666  0.889143  0.685191
2020-01-01 07:00:00  0.490666  0.677584  0.685191
2020-01-01 08:00:00  0.490666  0.466025  0.685191
2020-01-01 09:00:00  0.603678  0.254466  0.605424
2020-01-01 10:00:00  0.603678  0.358240  0.605424
2020-01-01 11:00:00  0.603678  0.462014  0.605424
2020-01-01 12:00:00  0.179458  0.565788  0.596706
2020-01-01 13:00:00  0.179458  0.477367  0.596706
2020-01-01 14:00:00  0.179458  0.388946  0.596706
2020-01-01 15:00:00  0.702992  0.300526  0.476644
2020-01-01 16:00:00  0.702992  0.516952  0.476644
2020-01-01 17:00:00  0.702992  0.733378  0.476644
2020-01-01 18:00:00  0.884276  0.949804  0.793237
2020-01-01 19:00:00  0.884276  0.907233  0.793237
2020-01-01 20:00:00  0.884276  0.864661  0.793237
2020-01-01 21:00:00  0.283859  0.822090  0.186542
2020-01-01 22:00:00  0.283859  0.834956  0.186542
2020-01-01 23:00:00  0.283859  0.847822  0.186542
2020-01-02 00:00:00  0.410897  0.860688  0.894249
本文链接:https://www.f2er.com/2934975.html

大家都在问