<small id='2oNzn'></small><noframes id='2oNzn'>

    <bdo id='2oNzn'></bdo><ul id='2oNzn'></ul>
  • <tfoot id='2oNzn'></tfoot>
    <legend id='2oNzn'><style id='2oNzn'><dir id='2oNzn'><q id='2oNzn'></q></dir></style></legend>

        <i id='2oNzn'><tr id='2oNzn'><dt id='2oNzn'><q id='2oNzn'><span id='2oNzn'><b id='2oNzn'><form id='2oNzn'><ins id='2oNzn'></ins><ul id='2oNzn'></ul><sub id='2oNzn'></sub></form><legend id='2oNzn'></legend><bdo id='2oNzn'><pre id='2oNzn'><center id='2oNzn'></center></pre></bdo></b><th id='2oNzn'></th></span></q></dt></tr></i><div id='2oNzn'><tfoot id='2oNzn'></tfoot><dl id='2oNzn'><fieldset id='2oNzn'></fieldset></dl></div>

      1. 给定日期时间列的 pandas 按周分组

        Pandas groupby week given a datetime column(给定日期时间列的 pandas 按周分组)

              <small id='XyV3C'></small><noframes id='XyV3C'>

              • <bdo id='XyV3C'></bdo><ul id='XyV3C'></ul>

                    <tbody id='XyV3C'></tbody>
                • <legend id='XyV3C'><style id='XyV3C'><dir id='XyV3C'><q id='XyV3C'></q></dir></style></legend>
                  <tfoot id='XyV3C'></tfoot>
                  <i id='XyV3C'><tr id='XyV3C'><dt id='XyV3C'><q id='XyV3C'><span id='XyV3C'><b id='XyV3C'><form id='XyV3C'><ins id='XyV3C'></ins><ul id='XyV3C'></ul><sub id='XyV3C'></sub></form><legend id='XyV3C'></legend><bdo id='XyV3C'><pre id='XyV3C'><center id='XyV3C'></center></pre></bdo></b><th id='XyV3C'></th></span></q></dt></tr></i><div id='XyV3C'><tfoot id='XyV3C'></tfoot><dl id='XyV3C'><fieldset id='XyV3C'></fieldset></dl></div>
                  本文介绍了给定日期时间列的 pandas 按周分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  假设我有以下数据示例:

                  df = pd.DataFrame({'date':['2011-01-01','2011-01-02',
                                         '2011-01-03','2011-01-04','2011-01-05',
                                         '2011-01-06','2011-01-07','2011-01-08',
                                         '2011-01-09','2011-12-30','2011-12-31'],
                                     'revenue':[5,3,2,
                                                10,12,2,
                                                1,0,6,10,12]})
                  
                  # Let's format the date and add the week number and year
                  df['date'] = pd.to_datetime(df['date'],format='%Y-%m-%d')
                  df['week_number'] = df['date'].dt.week
                  df['year'] = df['date'].dt.year
                  
                  df
                  
                          date        revenue     week_of_year    year
                  0       2011-01-01  5           52              2011
                  1       2011-01-02  3           52              2011
                  2       2011-01-03  2           1               2011
                  3       2011-01-04  10          1               2011
                  4       2011-01-05  12          1               2011
                  5       2011-01-06  2           1               2011
                  6       2011-01-07  1           1               2011
                  7       2011-01-08  0           1               2011
                  8       2011-01-09  6           1               2011
                  9       2011-12-30  10          52              2011
                  10      2011-12-31  12          52              2011
                  

                  我想计算每周的收入,以便稍后绘制结果,并分析时间序列。然后,预期输出将如下所示:

                      week    revenue
                  0   1       8
                  1   2       33
                  2   52      22
                  

                  我首先想到使用timestamp.week给出的周数。
                  但是,我想不出如何处理第1周之前一周的ISO周数定义。我有点困惑,因为在这种情况下,按week_number分组会将年初的收入和年底的收入相加。

                  推荐答案

                  当您使用dt.Week进行转换时,它是ISO week date。

                  您可以使用strftime

                  df.groupby(df.date.dt.strftime('%W')).revenue.sum()
                  Out[588]: 
                  date
                  00     8
                  01    33
                  52    22
                  Name: revenue, dtype: int64
                  

                  这篇关于给定日期时间列的 pandas 按周分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
                  Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
                  Is there a way of group by month in Pandas starting at specific day number?( pandas 有从特定日期开始的按月分组的方式吗?)
                  Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
                  Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
                  Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)
                    <legend id='G7rnI'><style id='G7rnI'><dir id='G7rnI'><q id='G7rnI'></q></dir></style></legend>

                      <small id='G7rnI'></small><noframes id='G7rnI'>

                        <bdo id='G7rnI'></bdo><ul id='G7rnI'></ul>
                            <tbody id='G7rnI'></tbody>
                          <i id='G7rnI'><tr id='G7rnI'><dt id='G7rnI'><q id='G7rnI'><span id='G7rnI'><b id='G7rnI'><form id='G7rnI'><ins id='G7rnI'></ins><ul id='G7rnI'></ul><sub id='G7rnI'></sub></form><legend id='G7rnI'></legend><bdo id='G7rnI'><pre id='G7rnI'><center id='G7rnI'></center></pre></bdo></b><th id='G7rnI'></th></span></q></dt></tr></i><div id='G7rnI'><tfoot id='G7rnI'></tfoot><dl id='G7rnI'><fieldset id='G7rnI'></fieldset></dl></div>
                          • <tfoot id='G7rnI'></tfoot>