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

        <bdo id='eIawE'></bdo><ul id='eIawE'></ul>
    1. <small id='eIawE'></small><noframes id='eIawE'>

      1. 如何在 Pandas 中使用总计(边距)创建数据透视?

        时间:2023-10-19
        <i id='1raYZ'><tr id='1raYZ'><dt id='1raYZ'><q id='1raYZ'><span id='1raYZ'><b id='1raYZ'><form id='1raYZ'><ins id='1raYZ'></ins><ul id='1raYZ'></ul><sub id='1raYZ'></sub></form><legend id='1raYZ'></legend><bdo id='1raYZ'><pre id='1raYZ'><center id='1raYZ'></center></pre></bdo></b><th id='1raYZ'></th></span></q></dt></tr></i><div id='1raYZ'><tfoot id='1raYZ'></tfoot><dl id='1raYZ'><fieldset id='1raYZ'></fieldset></dl></div>

            <tbody id='1raYZ'></tbody>
        1. <tfoot id='1raYZ'></tfoot>
          • <bdo id='1raYZ'></bdo><ul id='1raYZ'></ul>

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

                  <legend id='1raYZ'><style id='1raYZ'><dir id='1raYZ'><q id='1raYZ'></q></dir></style></legend>
                  本文介绍了如何在 Pandas 中使用总计(边距)创建数据透视?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  例如,我有一个非常简单的数据框:

                  For example, I have a very simple data frame:

                  values = pd.Series(i for i in range(5))
                  rows = pd.Series(['a', 'b', 'a', 'a', 'b'])
                  columns = pd.date_range('20130101',periods=5)
                  
                  df = pd.DataFrame({'values': values, 'rows': rows, 'columns': columns})
                  

                  以及它的外观:

                                columns rows  values
                  0 2013-01-01 00:00:00    a       0
                  1 2013-01-02 00:00:00    b       1
                  2 2013-01-03 00:00:00    a       2
                  3 2013-01-04 00:00:00    a       3
                  4 2013-01-05 00:00:00    b       4
                  

                  当我尝试在没有边距(总计)的情况下进行支点时,我取得了成功:

                  I have success when I try to make pivot without margins(totals):

                  pivot = pd.pivot_table(
                      data=df,
                      rows='rows',
                      cols='columns',
                      values='values',
                      margins=False
                  )
                  

                  它看起来如何:

                  columns  2013-01-01  2013-01-02  2013-01-03  2013-01-04  2013-01-05
                  rows                                                               
                  a                 0         NaN           2           3         NaN
                  b               NaN           1         NaN         NaN           4
                  

                  但如果我想创建带边距的枢轴:

                  but if I want create pivot with margins:

                  pivot = pd.pivot_table(
                      data=df,
                      rows='rows',
                      cols='columns',
                      values='values',
                      margins=True
                  )
                  

                  我收到错误:

                  Traceback (most recent call last):
                    File "./test.py", line 17, in <module>
                      margins=True
                    File "/usr/local/lib/python2.6/dist-packages/pandas/tools/pivot.py", line 135, in pivot_table
                      cols=cols, aggfunc=aggfunc)
                    File "/usr/local/lib/python2.6/dist-packages/pandas/tools/pivot.py", line 174, in _add_margins
                      piece[all_key] = margin[key]
                    File "/usr/local/lib/python2.6/dist-packages/pandas/core/frame.py", line 2119, in __setitem__
                      self._set_item(key, value)
                    File "/usr/local/lib/python2.6/dist-packages/pandas/core/frame.py", line 2166, in _set_item
                      NDFrame._set_item(self, key, value)
                    File "/usr/local/lib/python2.6/dist-packages/pandas/core/generic.py", line 679, in _set_item
                      self._data.set(key, value)
                    File "/usr/local/lib/python2.6/dist-packages/pandas/core/internals.py", line 1781, in set
                      self.insert(len(self.items), item, value)
                    File "/usr/local/lib/python2.6/dist-packages/pandas/core/internals.py", line 1801, in insert
                      new_items = self.items.delete(loc)
                    File "/usr/local/lib/python2.6/dist-packages/pandas/core/index.py", line 2610, in delete
                      new_labels = [np.delete(lab, loc) for lab in self.labels]
                    File "/usr/lib/pymodules/python2.6/numpy/lib/function_base.py", line 3339, in delete
                      "invalid entry")
                  ValueError: invalid entry
                  

                  • Python 版本:2.6.8
                  • 熊猫版本:0.12.0
                  • 系统:Debian Linux 3.2.0 内核,64 位.
                  • 谢谢.

                    推荐答案

                    我可以重现您的问题.这听起来像一个错误.至少我发现重新分配列名可以解决这个问题:

                    I can reproduce your issue. It sounds like a bug. At least I found that reassigning the column names workaround the issue:

                    df.columns = ['rows', 'columns', 'values']
                    
                    pd.pivot_table(
                        ...:     data=df,
                        ...:     rows='rows',
                        ...:     cols='columns',
                        ...:     values='values',
                        ...:     margins=True)
                    Out[34]: 
                    columns                     a    b  All
                    rows                                   
                    2013-01-01 00:00:00  0.000000  NaN    0
                    2013-01-02 00:00:00       NaN  1.0    1
                    2013-01-03 00:00:00  2.000000  NaN    2
                    2013-01-04 00:00:00  3.000000  NaN    3
                    2013-01-05 00:00:00       NaN  4.0    4
                    All                  1.666667  2.5    2
                    

                    这篇关于如何在 Pandas 中使用总计(边距)创建数据透视?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:需要转置一个 pandas 数据框 下一篇:Panda 数据透视表边距仅在行上

                  相关文章

                  1. <legend id='n0d7U'><style id='n0d7U'><dir id='n0d7U'><q id='n0d7U'></q></dir></style></legend>

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

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

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