按周分组Python Datafram的每日数据

2024-05-20 19:54:27 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我有一个Python数据帧,它按月份排序,然后按天排序

In [4]: result_GB_daily_average
Out[4]:
                NREL      Avert
Month Day
1     1    14.718417  37.250000
      2    40.381167  45.250000
      3    42.512646  40.666667
      4    12.166896  31.583333
      5    14.583208  50.416667
      6    34.238000  45.333333
      7    45.581229  29.125000
      8    60.548479  27.916667
      9    48.061583  34.041667
      10   20.606958  37.583333
      11    5.418833  70.833333
      12   51.261375  43.208333
      13   21.796771  42.541667
      14   27.118979  41.958333
      15    8.230542  43.625000
      16   14.233958  48.708333
      17   28.345875  51.125000
      18   43.896375  55.500000
      19   95.800542  44.500000
      20   53.763104  39.958333
      21   26.171437  50.958333
      22   20.372688  66.916667
      23   20.594042  42.541667
      24   16.889083  48.083333
      25   16.416479  42.125000
      26   28.459625  40.125000
      27    1.055229  49.833333
      28   36.798792  42.791667
      29   27.260083  47.041667
      30   23.584917  55.750000

一年中的每个月都会这样,我希望能够按周而不是按天进行排序,因此它看起来像这样:

^{pr2}$

等等。最简单的方法是什么?在


Tags: 数据方法in排序resultoutdailyaverage
1条回答
网友
1楼 · 发布于 2024-05-20 19:54:27

我想周不是指真正的日历周!!!以下是我提出的解决方案:

#First add a dummy column
result_GB_daily_average['count'] = 1

#Then calculate a cumulative sum and divide it by 7
result_GB_daily_average['Week'] = result_GB_daily_average['count'].cumsum() / 7.0

#Then Round the weeks
result_GB_daily_average['Week']=result_GB_daily_average['Week'].round()

#Then do the group by and calculate average
result_GB_week_average = result_GB_daily_average.groupby('Week')['NREL','AVERT'].mean()

相关问题 更多 >