python - 使用 python 重新采样数据帧

标签 python pandas dataframe resampling datetimeindex

我需要使用 pandas.DataFrame.resample 函数对数据帧重新采样,如下所示:

data.set_index('TIMESTAMP').resample('24min', how='sum')

这没有问题,但是当我尝试使用“xmin”调用函数时,其中 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

编辑2

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

当我这样调用这个函数时:

res = ratio_activ_equip(y, '2016-05-10 22:00:00', '2016-05-14 22:00:00', 30)

我收到此错误:

KeyError: 'TIMESTAMP'

请问有什么想法吗?

最佳答案

IIUC你需要传递变量xmin:

xmin = '24min'
data.set_index('TIMESTAMP').resample(xmin, how='sum')

更一般的是将 int 值转换为 str 并添加子字符串 min:

step = 10
xmin = str(step) + 'min'
data = data.set_index('TIMESTAMP').resample(xmin, how='sum')
<小时/>
step = 10
data = data.set_index('TIMESTAMP').resample(str(step) + 'min', how='sum')

关于python - 使用 python 重新采样数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39451021/

相关文章:

python - `np.ix_` 中的尾随下划线

python - 按行值拆分 pandas 数据框列

r - 如何选择 R 数据框中满足特定条件的第一行?

python - 为 Pandas 数据框中的下一个连续行添加新列

pandas 合并两个多级系列

Linux 上的 PYTHONPATH

python - Django 子进程从批处理脚本实时/无缓冲地报告标准输出

python - 将两行字符串打印为一行字符串 Tkinter

python - 将 pandas 数据帧转换为 dict,反之亦然

python - 如何在某个 ID 的 Pandas 列中获得第二高的值?