【问题标题】:Pandas sum (values from minutes to hour) time data values with PandasPandas 使用 Pandas 求和(从分钟到小时的值)时间数据值
【发布时间】:2022-01-15 18:36:00
【问题描述】:

我有一个问题:我想将时间数据值与 Pandas 相加。我有 15 分钟的数据,我想对一小时的值求和。

例如:我的数据

Index Date Time Value
0 11.06.2021 04:00 1125.6
1 11.06.2021 04:15 5622.2
2 11.06.2021 04:30 3222.6
3 11.06.2021 04:45 2666.7
4 11.06.2021 05:00 4657.2
5 11.06.2021 05:15 2946.8
6 11.06.2021 05:30 3255.4
7 11.06.2021 05:45 ...
8 11.06.2021 06:00 ...
9 11.06.2021 06:15 ...
10 11.06.2021 06:30 ...
... ... ... ...

我想拥有:

Index Date Time Value
0 11.06.2021 4:00 The sum from 4:00 to 4:45
1 11.06.2021 5:00 ...
2 11.06.2021 6:00 ...

这个数据叫做test

我的解决方案:

result = []
result1 = []
counter11 = 0
for index, row in test.iterrows():
    counter11 += 1
    print(counter11)
    result1 += test.values[index]
    result += result1
    if counter11 == 3:
        result.add(result.values)
        result = 0
        counter = 0
test["sum"] = result

如果有人可以帮助我,我会很高兴。谢谢。

【问题讨论】:

  • 这不是合适的方式,而是使用日期时间列。参考df.resample()
  • 编辑问题以将代码放入代码块中。
  • 抱歉,我是 StackOverflow 的新手,我必须收集一些有关 stackoverflow 的经验
  • 欢迎来到 SO!请熟悉markdown options

标签: python pandas dataframe date time-series


【解决方案1】:

使用:

  • 连接DateTime
  • pandas.to_datetime 转换为日期时间
  • set_index 到日期时间
  • resample 几个小时
  • summin_count=1 用于区分没有时间段的值。
import pandas as pd

# creating the dataframe
import io
data = """
Index,Date,Time,Value
0,11.06.2021,04:00,1125.6
1,11.06.2021,04:15,5622.2
2,11.06.2021,04:30,3222.6
3,11.06.2021,04:45,2666.7
4,11.06.2021,05:00,4657.2
5,11.06.2021,05:15,2946.8
6,11.06.2021,05:30,3255.4
7,11.06.2021,07:30,1111.1
"""
df = pd.read_csv(io.StringIO(data), sep=',', usecols=['Date', 'Time', 'Value'])

# processing
df['Datetime']= pd.to_datetime(df['Date']+' '+df['Time'])
df.drop(['Date', 'Time'], axis=1, inplace= True)
df.set_index('Datetime', inplace= True)
print(df.resample('H').sum(min_count=1))

# output
                       Value
Datetime                    
2021-11-06 04:00:00  12637.1
2021-11-06 05:00:00  10859.4
2021-11-06 06:00:00      NaN
2021-11-06 07:00:00   1111.1

【讨论】:

  • 您好,感谢您的解决方案。我加载了一个 csv 文件,您将数据声明为字符串。我可以将 data_time 也用于 csv 文件吗?
  • @Jacek,我编辑了答案以更紧密地匹配从 csv 文件创建数据框。用文件名替换 io.StringIO(data) 应该可以。但是这个版本假设 htat 文件的格式是 Index、Date、TimeValue。如果不是这种情况,请提供文件的子集。
  • 文件的子集是索引、日期、时间、值。时间和价值是分开的
  • @Edited 以匹配您的更改
  • 非常感谢您的帮助。我的问题是我在路径位置使用“”而不是“”
【解决方案2】:

如果您使用的是 pandas,我想提出两个建议。

首先,您应该使用 pandas 内置的 datetime,您可以在文档中找到有关它的所有内容:https://pandas.pydata.org/docs/user_guide/timeseries.html?highlight=datetime

这将使您在处理日期和时间时更加轻松。您可以使用 datetime 对象来创建和索引,如下面的代码示例所示。

第二个建议是,一旦你使用了 pandas 日期时间,你实际上可以使用其他内置函数,例如重采样。这将根据您提供给函数的一些逻辑“分组”数据框中的数据,例如按小时重新采样。然后,您可以将函数应用于该重新采样的数据,例如 sum()。

import pandas as pd
#This is just for making this example, you will not need numpy
import numpy as np

"This creates a datetime index"
idx = pd.date_range("2018-01-01", periods=70, freq="T")

"Creating a sample dataframe to work with"
df = pd.DataFrame(np.repeat(1,70), index=idx, columns= ["Data"])

"""
This is what you are trying to do:
Resampling or 'grouping' the data by hours (in this case) and sums all
values for each hour.
"""
df.resample("H").sum()

【讨论】:

  • 您好,感谢您的支持。我的问题是我从 csv 文件加载数据,而您从头开始创建 data_time。我有四个索引|日期 |时间 |单独列中的值。您从头开始创建 Data_Frame。我有一个从 11.06.2021 到 11.12.2021 的时间序列,时间步长为 15 分钟。我如何在不创建示例数据框的情况下解决他的代码示例问题
猜你喜欢
  • 1970-01-01
  • 2019-04-13
  • 1970-01-01
  • 2020-11-04
  • 2017-11-12
  • 1970-01-01
  • 1970-01-01
  • 2017-12-17
  • 2021-03-09
相关资源
最近更新 更多