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

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

    1. <tfoot id='ftwCn'></tfoot>

        pandas :从数据透视表中的另一列中减去一列

        Pandas: subtract one column from another in a pivot table( pandas :从数据透视表中的另一列中减去一列)
          <tbody id='9VaKL'></tbody>
        <legend id='9VaKL'><style id='9VaKL'><dir id='9VaKL'><q id='9VaKL'></q></dir></style></legend>

          <small id='9VaKL'></small><noframes id='9VaKL'>

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

                  本文介绍了 pandas :从数据透视表中的另一列中减去一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想从数据透视表中的另一列中减去一列.'diff' 应该是 2017 年和 2016 年之间的差异

                  I would like to subtract one columns from another in a pivot table. 'diff' shoud be the difference between 2017 and 2016

                  raw_data = {'year': [2016,2016,2017,2017],
                      'area': ['A','B','A','B'],
                      'age': [10,12,50,52]}
                  df1 = pd.DataFrame(raw_data, columns = ['year','area','age'])
                  
                  table=pd.pivot_table(df1,index=['area'],columns=['year'],values['age'],aggfunc='mean')
                  
                  table['diff']=table['2017']-table['2016']
                  

                  推荐答案

                  你需要删除 pivot_table 中的 [] 才能不创建 MultiIndex列:

                  You need remove [] in pivot_table for dont create MultiIndex in columns:

                  table=pd.pivot_table(df1,index='area',columns='year',values='age',aggfunc='mean')
                  print (table)
                  year  2016  2017
                  area            
                  A       10    50
                  B       12    52
                  
                  table['diff']=table[2017]-table[2016]
                  print (table)
                  year  2016  2017  diff
                  area                  
                  A       10    50    40
                  B       12    52    40
                  

                  另一个可能的解决方案是 droplevel:

                  Another possible solution is droplevel:

                  table=pd.pivot_table(df1,index=['area'],columns=['year'],values=['age'],aggfunc='mean')
                  table.columns = table.columns.droplevel(0)
                  print (table)
                  year  2016  2017
                  area            
                  A       10    50
                  B       12    52
                  

                  这篇关于 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替换为非空值)

                      • <bdo id='fgZY8'></bdo><ul id='fgZY8'></ul>
                      • <tfoot id='fgZY8'></tfoot><legend id='fgZY8'><style id='fgZY8'><dir id='fgZY8'><q id='fgZY8'></q></dir></style></legend>
                          • <small id='fgZY8'></small><noframes id='fgZY8'>

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

                              <tbody id='fgZY8'></tbody>