• <bdo id='AY0tT'></bdo><ul id='AY0tT'></ul>
    <tfoot id='AY0tT'></tfoot>

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

      2. <small id='AY0tT'></small><noframes id='AY0tT'>

      3. Pandas中的GROUP BY AND SUM不丢失列

        Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
        • <i id='77KKT'><tr id='77KKT'><dt id='77KKT'><q id='77KKT'><span id='77KKT'><b id='77KKT'><form id='77KKT'><ins id='77KKT'></ins><ul id='77KKT'></ul><sub id='77KKT'></sub></form><legend id='77KKT'></legend><bdo id='77KKT'><pre id='77KKT'><center id='77KKT'></center></pre></bdo></b><th id='77KKT'></th></span></q></dt></tr></i><div id='77KKT'><tfoot id='77KKT'></tfoot><dl id='77KKT'><fieldset id='77KKT'></fieldset></dl></div>
          • <bdo id='77KKT'></bdo><ul id='77KKT'></ul>
            <legend id='77KKT'><style id='77KKT'><dir id='77KKT'><q id='77KKT'></q></dir></style></legend><tfoot id='77KKT'></tfoot>
                <tbody id='77KKT'></tbody>

              <small id='77KKT'></small><noframes id='77KKT'>

                  本文介绍了Pandas中的GROUP BY AND SUM不丢失列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

                  --------------------------------------------------------------------
                  |TradeGroup | Fund Name | Contribution | From       | To           |
                  |  A        | Fund_1    |   0.20       | 2013-01-01 | 2013-01-02   |
                  |  B        | Fund_1    |   0.10       | 2013-01-01 | 2013-01-02   |
                  |  A        | Fund_1    |   0.05       | 2013-01-03 | 2013-01-04   |
                  |  B        | Fund_1    |   0.45       | 2013-01-03 | 2013-01-04   |
                  --------------------------------------------------------------------
                  

                  基本上,这是一个行业团体每天向基金捐款。我想要做的是总结一个交易团每天的所有贡献,以供进一步分析。 我想看到的是:

                  --------------------------------------------------------------------
                  |TradeGroup | Fund Name | Contribution | From       | To           |
                  |  A        | Fund_1    |   0.25       | 2013-01-01 | 2013-01-04   |
                  |  B        | Fund_1    |   0.55       | 2013-01-01 | 2013-01-04   |
                  --------------------------------------------------------------------
                  

                  我无法使用Dataframe解决此问题。我已经试过

                  df.groupby('TradeGroup')['Contribution'].sum()
                  

                  但是,这不起作用。与此等效的SQL将为

                  Select SUM(Ctp) from Table Group By TradeGroup. 
                  

                  任何帮助都将不胜感激。谢谢

                  sql

                  您需要确保贡献列是数字,而不是字符串,才能获得正确的匹配数字,就像在推荐答案中一样。我认为你收到的奇怪的"不"是因为你"投稿"栏目的字符串性质。则应执行以下操作:

                  import pandas as pd
                  import numpy as np
                  a=pd.DataFrame([['A','Fund_1','0.20','2013-01-01','2013-01-02'],
                  ['B','Fund_1','0.10','2013-01-01','2013-01-02'],['A','Fund_1','0.05','2013-
                  01-03','2013-01-04'],['B','Fund_1','0.45','2013-01-03','2013-01-04']],
                              columns=['TraderGroup', 'Fund Name','Contribution','From', 'To'])
                  print a
                  a['Contribution'] = pd.to_numeric(a['Contribution'], errors='coerce')
                  b=a.groupby(['TraderGroup','Fund Name']).agg({'Contribution':np.sum,
                                                           'From':'min','To':'max'}).reset_index()
                  print b
                  

                  这篇关于Pandas中的GROUP BY AND SUM不丢失列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
                  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替换为非空值)
                  Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)

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

                    <bdo id='vCu7S'></bdo><ul id='vCu7S'></ul>

                              <tbody id='vCu7S'></tbody>
                            <legend id='vCu7S'><style id='vCu7S'><dir id='vCu7S'><q id='vCu7S'></q></dir></style></legend>
                            <tfoot id='vCu7S'></tfoot>
                          1. <i id='vCu7S'><tr id='vCu7S'><dt id='vCu7S'><q id='vCu7S'><span id='vCu7S'><b id='vCu7S'><form id='vCu7S'><ins id='vCu7S'></ins><ul id='vCu7S'></ul><sub id='vCu7S'></sub></form><legend id='vCu7S'></legend><bdo id='vCu7S'><pre id='vCu7S'><center id='vCu7S'></center></pre></bdo></b><th id='vCu7S'></th></span></q></dt></tr></i><div id='vCu7S'><tfoot id='vCu7S'></tfoot><dl id='vCu7S'><fieldset id='vCu7S'></fieldset></dl></div>