gpt4 book ai didi

python - 我怎样才能在 python 中获得每日平均值?

转载 作者:行者123 更新时间:2023-11-28 22:23:34 27 4
gpt4 key购买 nike

我有一个格式如下的文件:

(年-月-日-数据)

1980 - 1 - 1 - 1.2
1980 - 1 - 2 - 1.3
1980 - 1 - 3 - 1.4
1980 - 1 - 4 - 1.5
1980 - 1 - 5 - 1.6
1980 - 1 - 6 - 1.7
1980 - 1 - 7 - 1.8

它在一个 numpy 数组中。这是大约 24 年的数据,所以我想做的是取每天的平均值并将其放入一个单独的一维数组中,该数组仅为 366(闰年)平均值,我可以然后使用 matplotlib 绘图并能够看到多年来的趋势。如果无论如何都可以在循环中使用子集化,那么我可以完成这个吗?

最佳答案

使用 pandas 绝对是最佳选择。至少有两种方法可以按“一年中的某一天”进行分组,您可以将一年中的数字日期作为字符串或字符串 monthday 组合,如下所示:

import pandas as pd
import numpy as np

df = pd.DataFrame(index=pd.date_range('2000-01-01', '2010-12-31'))

df['vals'] = np.random.randint(1, 6, df.shape[0])

print(df.groupby(df.index.strftime("%j")).mean())
print(df.groupby(df.index.strftime("%m%d")).mean())

关于python - 我怎样才能在 python 中获得每日平均值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46916993/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com