<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 中使用总计(边距)创建数据透视?

        How to create pivot with totals (margins) in Pandas?(如何在 Pandas 中使用总计(边距)创建数据透视?)
        <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 中使用总计(边距)创建数据透视?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Running .jl file from R or Python(从 R 或 Python 运行 .jl 文件)
                  Running Julia .jl file in python(在 python 中运行 Julia .jl 文件)
                  Using PIP in a Azure WebApp(在 Azure WebApp 中使用 PIP)
                  How to run python3.7 based flask web api on azure(如何在 azure 上运行基于 python3.7 的烧瓶 web api)
                  Azure Python Web App Internal Server Error(Azure Python Web 应用程序内部服务器错误)
                  Run python dlib library on azure app service(在 azure app 服务上运行 python dlib 库)

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

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

                        <tfoot id='n0d7U'></tfoot>
                        • <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'>