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

      <tfoot id='8SFpd'></tfoot>

      <small id='8SFpd'></small><noframes id='8SFpd'>

      • <bdo id='8SFpd'></bdo><ul id='8SFpd'></ul>

      将 pandas DataFrame 旋转为正确的格式:`DataError: No numeric types to

      Pivot a pandas DataFrame to be the correct format: `DataError: No numeric types to aggregate`(将 pandas DataFrame 旋转为正确的格式:`DataError: No numeric types to aggregate`)

      <tfoot id='cuVj6'></tfoot>

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

              • <legend id='cuVj6'><style id='cuVj6'><dir id='cuVj6'><q id='cuVj6'></q></dir></style></legend>
              • 本文介绍了将 pandas DataFrame 旋转为正确的格式:`DataError: No numeric types to aggregate`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                这是我想要操作的 pandas DataFrame:

                Here is a pandas DataFrame I would like to manipulate:

                import pandas as pd
                
                data = {"grouping": ["item1", "item1", "item1", "item2", "item2", "item2", "item2", ...],
                        "labels": ["A", "B", "C", "A", "B", "C", "D", ...],
                        "count": [5, 1, 8, 3, 731, 189, 9, ...]}
                
                df = pd.DataFrame(data)
                
                print(df)
                >>>   grouping            labels       count
                0        item1             A            5
                1        item1             B            1
                2        item1             C            8
                3        item2             A            3
                4        item2             B          731
                5        item2             C          189
                6        item2             D            9
                7        ...               ...         ....
                

                我想将此数据框展开"为以下格式:

                I would like to "unfold" this dataframe into the following format:

                grouping    A    B    C    D
                item1       5    1    8    3
                item2       3    731  189  9
                ....        ........
                

                如何做到这一点?我认为这会起作用:

                How would one do this? I would think that this would work:

                pd.pivot_table(df,index=["grouping", "labels"]
                

                但我收到以下错误:

                DataError: No numeric types to aggregate
                

                推荐答案

                有四种惯用的 pandas 方法可以做到这一点.

                There are four idiomatic pandas ways to do this.

                • 分组列之间没有重复.不需要聚合
                  • 枢轴
                  • set_index
                  • 数据透视表
                  • 分组方式

                  枢轴

                  df.pivot('grouping', 'labels', 'count')
                  

                  set_index

                  df.set_index(['grouping', 'labels'])['count'].unstack()
                  

                  pivot_table

                  df.pivot_table('count', 'grouping', 'labels')
                  

                  groupby

                  df.groupby(['grouping', 'labels'])['count'].sum().unstack()
                  

                  全部收益

                  labels      A      B      C    D
                  grouping                        
                  item1     5.0    1.0    8.0  NaN
                  item2     3.0  731.0  189.0  9.0
                  

                  时机

                  使用 groupbyset_indexpivot_table 方法,您可以使用 fill_value=0

                  With the groupby, set_index, or pivot_table approach, you can easily fill in missing values with fill_value=0

                  df.pivot_table('count', 'grouping', 'labels', fill_value=0)
                  
                  df.groupby(['grouping', 'labels'])['count'].sum().unstack(fill_value=0)
                  
                  df.set_index(['grouping', 'labels'])['count'].sum().unstack(fill_value=0)
                  

                  全部收益

                  labels    A    B    C  D
                  grouping                
                  item1     5    1    8  0
                  item2     3  731  189  9
                  

                  <小时>

                  关于groupby的其他想法

                  因为我们不需要任何聚合.如果我们想使用 groupby,我们可以通过使用影响较小的聚合器来最小化隐式聚合的影响.

                  Because we don't require any aggregation. If we wanted to use groupby, we can minimize the impact of the implicit aggregation by utilizing a less impactful aggregator.

                  df.groupby(['grouping', 'labels'])['count'].max().unstack()
                  

                  df.groupby(['grouping', 'labels'])['count'].first().unstack()
                  

                  定时groupby

                  这篇关于将 pandas DataFrame 旋转为正确的格式:`DataError: No numeric types to aggregate`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                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 库)
                <tfoot id='R68CO'></tfoot>
                  <tbody id='R68CO'></tbody>

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

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

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

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