本文介绍了如何从当月的每一天减去一个月的平均值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个每天 55 年的时间序列.我已经找到了每年每个月的月平均值.现在我想从那个月和那年的相应天数中减去这个月平均值.

I have a time-series of 55 years at a daily scale. I have found the monthly mean of each month for each year. Now I want to subtract this monthly mean from the corresponding days of that month and year.

我的熊猫数据框如下所示:

My pandas data frame looks like this:

                 0    1       2       3  ...       5       6         7        8
Date                                     ...
1951-01-01  28.361  0.0  131.24  405.39  ...  405.39  38.284  0.187010 -1.23550
1951-01-02  27.874  0.0  113.74  409.56  ...  409.56  49.834  0.066903 -1.44770
           ...  ...     ...     ...  ...     ...     ...       ...      ...
2005-12-16  27.921  0.0  104.99  429.78  ...  429.78  47.529 -1.814300 -5.47720
2005-12-17  27.918  0.0  112.11  425.32  ...  425.32  46.541 -3.314000 -4.02050

我计算了一年中每个月的平均值,如下所示:

I calculated the mean for each month of a year as shown below:

                    0          1           2  ...          6         7         8
Date                                          ...
1951-01-31  28.833387   0.000000  115.779677  ...  44.500613 -0.118354 -1.036190
1951-02-28  31.317429   0.000000  118.854179  ...  39.712607  0.802474 -2.443536
1951-03-31  34.550839   0.525103   86.424677  ...  43.239742  1.450830 -2.713229
              ...        ...         ...  ...        ...       ...       ...
2005-11-30  27.742767  12.357321   95.257233  ...  48.338700 -0.926430  0.250278
2005-12-31  28.171647   0.569575  106.816765  ...  43.437294 -0.998577 -1.632173

现在我想减去 1951 年 1 月的平均值到 1951 年 1 月的所有天数,以此类推,以得到整个时间序列.

Now I want to subtract the mean of January 1951 to all days of January 1951 and so on for entire time-series.

我无法考虑如何进行.

推荐答案

使用 GroupBy.transformDataFrame.sub

#if neccesary
#df.index = pd.to_datetime(df.index)
df.sub(df.groupby([df.index.month,df.index.year]).transform('mean'))

Resampler.transform

df.sub(df.resample('M').transform('mean'))

这篇关于如何从当月的每一天减去一个月的平均值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 23:44