<tfoot id='ayMvw'></tfoot>

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

  • <legend id='ayMvw'><style id='ayMvw'><dir id='ayMvw'><q id='ayMvw'></q></dir></style></legend>

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

      1. pandas:如何使用多索引运行数据透视?

        时间:2023-09-28

          <tbody id='d5FVm'></tbody>
        • <bdo id='d5FVm'></bdo><ul id='d5FVm'></ul>

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

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

                • <legend id='d5FVm'><style id='d5FVm'><dir id='d5FVm'><q id='d5FVm'></q></dir></style></legend>

                  本文介绍了pandas:如何使用多索引运行数据透视?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想在 pandas DataFrame 上运行一个支点,索引是两列,而不是一列.例如,一个字段用于年份,一个用于月份,一个item"字段显示item 1"和item 2",以及一个带有数值的value"字段.我希望索引为年 + 月.

                  I would like to run a pivot on a pandas DataFrame, with the index being two columns, not one. For example, one field for the year, one for the month, an 'item' field which shows 'item 1' and 'item 2' and a 'value' field with numerical values. I want the index to be year + month.

                  我设法使它工作的唯一方法是将两个字段合并为一个,然后再次将它们分开.有没有更好的办法?

                  The only way I managed to get this to work was to combine the two fields into one, then separate them again. is there a better way?

                  下面复制的最小代码.非常感谢!

                  Minimal code copied below. Thanks a lot!

                  PS 是的,我知道关键字pivot"和multi-index"还有其他问题,但我不明白他们是否/如何帮助我解决这个问题.

                  PS Yes, I am aware there are other questions with the keywords 'pivot' and 'multi-index', but I did not understand if/how they can help me with this question.

                  import pandas as pd
                  import numpy as np
                  
                  df= pd.DataFrame()
                  month = np.arange(1, 13)
                  values1 = np.random.randint(0, 100, 12)
                  values2 = np.random.randint(200, 300, 12)
                  
                  
                  df['month'] = np.hstack((month, month))
                  df['year'] = 2004
                  df['value'] = np.hstack((values1, values2))
                  df['item'] = np.hstack((np.repeat('item 1', 12), np.repeat('item 2', 12)))
                  
                  # This doesn't work: 
                  # ValueError: Wrong number of items passed 24, placement implies 2
                  # mypiv = df.pivot(['year', 'month'], 'item', 'value')
                  
                  # This doesn't work, either:
                  # df.set_index(['year', 'month'], inplace=True)
                  # ValueError: cannot label index with a null key
                  # mypiv = df.pivot(columns='item', values='value')
                  
                  # This below works but is not ideal: 
                  # I have to first concatenate then separate the fields I need
                  df['new field'] = df['year'] * 100 + df['month']
                  
                  mypiv = df.pivot('new field', 'item', 'value').reset_index()
                  mypiv['year'] = mypiv['new field'].apply( lambda x: int(x) / 100)  
                  mypiv['month'] = mypiv['new field'] % 100
                  

                  推荐答案

                  你可以分组然后unstack.

                  You can group and then unstack.

                  >>> df.groupby(['year', 'month', 'item'])['value'].sum().unstack('item')
                  item        item 1  item 2
                  year month                
                  2004 1          33     250
                       2          44     224
                       3          41     268
                       4          29     232
                       5          57     252
                       6          61     255
                       7          28     254
                       8          15     229
                       9          29     258
                       10         49     207
                       11         36     254
                       12         23     209
                  

                  或者使用pivot_table:

                  >>> df.pivot_table(
                          values='value', 
                          index=['year', 'month'], 
                          columns='item', 
                          aggfunc=np.sum)
                  item        item 1  item 2
                  year month                
                  2004 1          33     250
                       2          44     224
                       3          41     268
                       4          29     232
                       5          57     252
                       6          61     255
                       7          28     254
                       8          15     229
                       9          29     258
                       10         49     207
                       11         36     254
                       12         23     209
                  

                  这篇关于pandas:如何使用多索引运行数据透视?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:pandas :pivot 和 pivot_table 之间的区别.为什么只有 pivot_table 工作? 下一篇:如何传递 argparse 参数以充当 kwargs?

                  相关文章

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

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

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