使用python对数据帧重新采样
我需要使用pandas.dataframe.resample函数对数据帧重新采样,如下所示:使用python对数据帧重新采样,python,pandas,dataframe,resampling,datetimeindex,Python,Pandas,Dataframe,Resampling,Datetimeindex,我需要使用pandas.dataframe.resample函数对数据帧重新采样,如下所示: data.set_index('TIMESTAMP').resample('24min', how='sum') res = ratio_activ_equip(y, '2016-05-10 22:00:00', '2016-05-14 22:00:00', 30) 这没有问题,但是当我尝试用'xmin'调用function时,其中x是一个通用参数 data.set_index('TIMESTA
data.set_index('TIMESTAMP').resample('24min', how='sum')
res = ratio_activ_equip(y, '2016-05-10 22:00:00', '2016-05-14 22:00:00', 30)
这没有问题,但是当我尝试用'xmin'调用function时,其中x是一个通用参数
data.set_index('TIMESTAMP').resample('xmin', how='sum')
这是行不通的
有什么想法吗
多谢各位
编辑
def ratio_activ_equip(data, date_deb, date_fin, step):
# filtre_site(data, site)
filtre_date(data, date_deb, date_fin)
xmin = 'stepmin'
data.set_index('TIMESTAMP').resample(xmin, how='sum')
res = data.iloc[:,1:10] = data.iloc[:,1:10].divide(data.sum(axis=1), axis=0)
res = data
return res
EDIT2
def ratio_activ_equip(data, date_deb, date_fin, step): #
# filtre_site(data, site)
filtre_date(data, date_deb, date_fin)
#step = 10
xmin = str(step) + 'min'
data = data.set_index('TIMESTAMP').resample(xmin, how='sum')
data.set_index('TIMESTAMP').resample(xmin, how='sum')
res = data.iloc[:,1:10] = data.iloc[:,1:10].divide(data.sum(axis=1), axis=0)
res = data
return res
当我这样称呼它的时候:
data.set_index('TIMESTAMP').resample('24min', how='sum')
res = ratio_activ_equip(y, '2016-05-10 22:00:00', '2016-05-14 22:00:00', 30)
我得到这个错误:
有什么想法吗?IIUC您需要传递变量
xmin
:
xmin = '24min'
data.set_index('TIMESTAMP').resample(xmin, how='sum')
step = 10
xmin = str(step) + 'min'
data = data.set_index('TIMESTAMP').resample(xmin, how='sum')
更通用的方法是将int
值转换为str
并添加子字符串min
:
xmin = '24min'
data.set_index('TIMESTAMP').resample(xmin, how='sum')
step = 10
xmin = str(step) + 'min'
data = data.set_index('TIMESTAMP').resample(xmin, how='sum')
谢谢你,jezrael,但我需要做得更一般一些,我编辑了我的帖子。在这种情况下,我如何使用step变量对数据帧进行重采样?Phhhooo,
resample
没有参数stepmin
。你需要从1Min
到24Min
进行重采样吗?我取决于具体情况,甚至可能需要500分钟。这里是打字错误,你需要重新采样移动数据。设置索引(“时间戳”)。重新采样(xmin,how='sum')