如何将5分钟的间隔数据更改为每小时间隔

发布于 2025-02-02 10:54:32 字数 223 浏览 4 评论 0原文

因此,我每5分钟都有每5分钟记录在恒温器上的温度数据。我想更改数据,以便文件仅具有每小时记录的数据。我知道有一种方法可以分组数据,但我不想要总和/最大/AVG等。我希望每小时温度。让我知道这是否有意义,以及您是否需要其他任何东西。 数据看起来像这样

So I have data of the temperature recorded on a thermostat every 5 minutes. I would like to change the data so that the file has only the data recorded every hour. I know there is a way to group the data but I don't want the sum/min/max/avg etc. I want the temperature for each hour. Let me know if this makes sense and if you need anything else.
The data looks like this

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

穿透光 2025-02-09 10:54:33

这是我使用一些随机生成的数据提出的解决方案。我只是将数据框过滤到series.dt.minute属性等于0的位置。

import pandas as pd
import datetime
import numpy as np

rng = np.random.default_rng()

test_list = []

date_time = datetime.datetime.strptime('2022-05-26', '%Y-%m-%d')
for i in range(30):
    date_time += datetime.timedelta(minutes=5)
    new_row = {'datetime': date_time, 'temp': rng.integers(low=30, high=95, size=1)[0]}
    test_list.append(new_row)

test_df = pd.DataFrame(test_list)
hourly_df = test_df[test_df['datetime'].dt.minute == 0]

Here is a solution I came up with using some randomly generated data. I just filtered the data frame to where the Series.dt.minute attribute equals 0.

import pandas as pd
import datetime
import numpy as np

rng = np.random.default_rng()

test_list = []

date_time = datetime.datetime.strptime('2022-05-26', '%Y-%m-%d')
for i in range(30):
    date_time += datetime.timedelta(minutes=5)
    new_row = {'datetime': date_time, 'temp': rng.integers(low=30, high=95, size=1)[0]}
    test_list.append(new_row)

test_df = pd.DataFrame(test_list)
hourly_df = test_df[test_df['datetime'].dt.minute == 0]
身边 2025-02-09 10:54:32

Python的最简单方法是使用Pandas软件包。如果您没有它:

pip install pandas

然后在脚本中导入它,并使用它来过滤第一列中不在小时的所有日期值。让我知道这是否对您有用。

import pandas as pd

df = pd.read_csv("name.csv")
df1 = df[df['DateTime'].dt.strftime('%M:%S').eq('00:00')].copy()
print(df1)
df1.to_csv(index=False)

Simplest way with python would be with the pandas package. If you don't have it:

pip install pandas

Then in your script import it and use it to filter out all date values in the first column that are not on the hour. Let me know if this works for you.

import pandas as pd

df = pd.read_csv("name.csv")
df1 = df[df['DateTime'].dt.strftime('%M:%S').eq('00:00')].copy()
print(df1)
df1.to_csv(index=False)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文