<tfoot id='vb037'></tfoot>

    1. <small id='vb037'></small><noframes id='vb037'>

        <bdo id='vb037'></bdo><ul id='vb037'></ul>
    2. <legend id='vb037'><style id='vb037'><dir id='vb037'><q id='vb037'></q></dir></style></legend>

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

        如何使用Python/Pandas从日期字段按月分组

        How can I Group By Month from a Date field using Python/Pandas(如何使用Python/Pandas从日期字段按月分组)
          <legend id='Mlapc'><style id='Mlapc'><dir id='Mlapc'><q id='Mlapc'></q></dir></style></legend>

                <bdo id='Mlapc'></bdo><ul id='Mlapc'></ul>
              • <small id='Mlapc'></small><noframes id='Mlapc'>

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

                  <tbody id='Mlapc'></tbody>

                  本文介绍了如何使用Python/Pandas从日期字段按月分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个数据帧DF,如下所示:

                  | date      | Revenue |
                  |-----------|---------|
                  | 6/2/2017  | 100     |
                  | 5/23/2017 | 200     |
                  | 5/20/2017 | 300     |
                  | 6/22/2017 | 400     |
                  | 6/21/2017 | 500     |
                  

                  以上数据需要按月分组输出为:

                  | date | SUM(Revenue) |
                  |------|--------------|
                  | May  | 500          |
                  | June | 1000         |
                  

                  我尝试了此代码,但不起作用:

                  df.groupby(month('date')).agg({'Revenue': 'sum'})
                  

                  我只想使用Pandas或Numpy,不使用附加库

                  推荐答案

                  尝试此操作:

                  In [6]: df['date'] = pd.to_datetime(df['date'])
                  
                  In [7]: df
                  Out[7]: 
                          date  Revenue
                  0 2017-06-02      100
                  1 2017-05-23      200
                  2 2017-05-20      300
                  3 2017-06-22      400
                  4 2017-06-21      500
                  
                  
                  
                  In [59]: df.groupby(df['date'].dt.strftime('%B'))['Revenue'].sum().sort_values()
                  Out[59]: 
                  date
                  May      500
                  June    1000
                  

                  这篇关于如何使用Python/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='Lqs6h'><style id='Lqs6h'><dir id='Lqs6h'><q id='Lqs6h'></q></dir></style></legend>
                  1. <small id='Lqs6h'></small><noframes id='Lqs6h'>

                      <tbody id='Lqs6h'></tbody>

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

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