熊猫替换每日观察每月平均值

发布于 2025-02-13 07:30:54 字数 618 浏览 0 评论 0原文

假设,我有一个带有每日观察的熊猫系列:

pd_series = pd.Series(np.random.rand(26281), index = pd.date_range('2022-01-01', '2024-12-31', freq = 'H'))
pd_series
2022-01-01 00:00:00    0.933746
2022-01-01 01:00:00    0.588907
2022-01-01 02:00:00    0.229040
2022-01-01 03:00:00    0.557752
2022-01-01 04:00:00    0.798649
  
2024-12-30 20:00:00    0.314143
2024-12-30 21:00:00    0.670485
2024-12-30 22:00:00    0.300531
2024-12-30 23:00:00    0.075403
2024-12-31 00:00:00    0.716685

我想要的是将每个观察结果替换为每月平均值。我知道可以计算出平均值,

pd_series.resample('MS').mean()

但是如何将观察值对各自的观察结果进行呢?

Suppose, I have a pandas Series with daily observations:

pd_series = pd.Series(np.random.rand(26281), index = pd.date_range('2022-01-01', '2024-12-31', freq = 'H'))
pd_series
2022-01-01 00:00:00    0.933746
2022-01-01 01:00:00    0.588907
2022-01-01 02:00:00    0.229040
2022-01-01 03:00:00    0.557752
2022-01-01 04:00:00    0.798649
  
2024-12-30 20:00:00    0.314143
2024-12-30 21:00:00    0.670485
2024-12-30 22:00:00    0.300531
2024-12-30 23:00:00    0.075403
2024-12-31 00:00:00    0.716685

What I want is to replace every observation by the monthly average. I know that the average can be calculated as

pd_series.resample('MS').mean()

But how do I put the observations to the respective observations?

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

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

发布评论

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

评论(1

初懵 2025-02-20 07:30:54

使用 resampler。变换

print (pd_series.resample('MS').transform('mean'))
2022-01-01 00:00:00    0.495015
2022-01-01 01:00:00    0.495015
2022-01-01 02:00:00    0.495015
2022-01-01 03:00:00    0.495015
2022-01-01 04:00:00    0.495015
  
2024-12-30 20:00:00    0.508646
2024-12-30 21:00:00    0.508646
2024-12-30 22:00:00    0.508646
2024-12-30 23:00:00    0.508646
2024-12-31 00:00:00    0.508646
Freq: H, Length: 26281, dtype: float64

Use Resampler.transform:

print (pd_series.resample('MS').transform('mean'))
2022-01-01 00:00:00    0.495015
2022-01-01 01:00:00    0.495015
2022-01-01 02:00:00    0.495015
2022-01-01 03:00:00    0.495015
2022-01-01 04:00:00    0.495015
  
2024-12-30 20:00:00    0.508646
2024-12-30 21:00:00    0.508646
2024-12-30 22:00:00    0.508646
2024-12-30 23:00:00    0.508646
2024-12-31 00:00:00    0.508646
Freq: H, Length: 26281, dtype: float64
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文