pandas 如何添加连续两个小时之间发生的值?

y0u0uwnf  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(87)

我有一个df如下:

dates   values
2020-01-01 00:15:00 87.321
2020-01-01 00:30:00 87.818
2020-01-01 00:45:00 88.514
2020-01-01 01:00:00 89.608
2020-01-01 01:15:00 90.802
2020-01-01 01:30:00 91.896
2020-01-01 01:45:00 92.393
2020-01-01 02:00:00 91.995
2020-01-01 02:15:00 90.504
2020-01-01 02:30:00 88.216
2020-01-01 02:45:00 85.929
2020-01-01 03:00:00 84.238

字符串
当分钟是00时,我只想保留小时值,而在此之前发生的values必须相加。
范例:为了找到2020-01-01 01:00:00处的值,应从2020-01-01 00:15:002020-01-01 01:00:00添加值(87.321+87.818+88.514+59.608 = 353.261)。类似地,为了找到2020-01-01 02:00:00处的值,2020-01-01 01:15:002020-01-01 02:00:00的值应相加(90.802+91.896+92.393+91.995 = 348.887)

期望输出

dates  values
 2020-01-01 01:00:00    353.261
 2020-01-01 02:00:00    348.887
 2020-01-01 03:00:00    333.67


我使用df['dates'].dt.minute.eq(0)来获得布尔掩码,但我无法找到添加它们的方法。

fivyi3re

fivyi3re1#

hourly = df.set_index('dates') \  # Set the dates as index
           .resample('1H', closed='right', label='right') \  # Resample, so that you have one value for each hour
           .sum()  # Set the sum of values as new value

hourly = hourly.reset_index()  # If you want to have the dates as column again

字符串

相关问题