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

      <legend id='lrIrF'><style id='lrIrF'><dir id='lrIrF'><q id='lrIrF'></q></dir></style></legend>

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

      <i id='lrIrF'><tr id='lrIrF'><dt id='lrIrF'><q id='lrIrF'><span id='lrIrF'><b id='lrIrF'><form id='lrIrF'><ins id='lrIrF'></ins><ul id='lrIrF'></ul><sub id='lrIrF'></sub></form><legend id='lrIrF'></legend><bdo id='lrIrF'><pre id='lrIrF'><center id='lrIrF'></center></pre></bdo></b><th id='lrIrF'></th></span></q></dt></tr></i><div id='lrIrF'><tfoot id='lrIrF'></tfoot><dl id='lrIrF'><fieldset id='lrIrF'></fieldset></dl></div>
      • <bdo id='lrIrF'></bdo><ul id='lrIrF'></ul>
    2. 使用Pandas GroupBy和VALUE_COUNTS查找最常用的值

      Finding most common values with Pandas GroupBy and value_counts(使用Pandas GroupBy和VALUE_COUNTS查找最常用的值)
      <i id='MCvAx'><tr id='MCvAx'><dt id='MCvAx'><q id='MCvAx'><span id='MCvAx'><b id='MCvAx'><form id='MCvAx'><ins id='MCvAx'></ins><ul id='MCvAx'></ul><sub id='MCvAx'></sub></form><legend id='MCvAx'></legend><bdo id='MCvAx'><pre id='MCvAx'><center id='MCvAx'></center></pre></bdo></b><th id='MCvAx'></th></span></q></dt></tr></i><div id='MCvAx'><tfoot id='MCvAx'></tfoot><dl id='MCvAx'><fieldset id='MCvAx'></fieldset></dl></div>

    3. <small id='MCvAx'></small><noframes id='MCvAx'>

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

                  <tbody id='MCvAx'></tbody>
              1. <tfoot id='MCvAx'></tfoot>

                <legend id='MCvAx'><style id='MCvAx'><dir id='MCvAx'><q id='MCvAx'></q></dir></style></legend>
                本文介绍了使用Pandas GroupBy和VALUE_COUNTS查找最常用的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在使用表中的两列。

                +-------------+--------------------------------------------------------------+
                |  Area Name  |                       Code Description                       |
                +-------------+--------------------------------------------------------------+
                | N Hollywood | VIOLATION OF RESTRAINING ORDER                               |
                | N Hollywood | CRIMINAL THREATS - NO WEAPON DISPLAYED                       |
                | N Hollywood | CRIMINAL THREATS - NO WEAPON DISPLAYED                       |
                | N Hollywood | ASSAULT WITH DEADLY WEAPON, AGGRAVATED ASSAULT               |
                | Southeast   | ASSAULT WITH DEADLY WEAPON, AGGRAVATED ASSAULT               |
                | West Valley | CRIMINAL THREATS - NO WEAPON DISPLAYED                       |
                | West Valley | CRIMINAL THREATS - NO WEAPON DISPLAYED                       |
                | 77th Street | RAPE, FORCIBLE                                               |
                | Foothill    | CRM AGNST CHLD (13 OR UNDER) (14-15 & SUSP 10 YRS OLDER)0060 |
                | N Hollywood | VANDALISM - FELONY ($400 & OVER, ALL CHURCH VANDALISMS) 0114 |
                +-------------+--------------------------------------------------------------+
                

                我正在使用Groupby和Value_Counts按区域名称查找代码说明。

                df.groupby(['Area Name'])['Code Description'].value_counts()
                

                有没有办法只查看每个区域名称的前‘n’个值?如果我将.nlargest(3)追加到上面的代码,它只返回一个区域名称的结果。

                +---------------------------------------------------------------------------------+
                | Wilshire     SHOPLIFTING-GRAND THEFT ($950.01 & OVER)                         7 |
                +---------------------------------------------------------------------------------+
                

                推荐答案

                使用value_counts结果中的head每组:

                df.groupby('Area Name')['Code Description'].apply(lambda x: x.value_counts().head(3))
                

                输出:

                Area Name                                                                
                77th Street  RAPE, FORCIBLE                                                  1
                Foothill     CRM AGNST CHLD (13 OR UNDER) (14-15 & SUSP 10 YRS OLDER)0060    1
                N Hollywood  CRIMINAL THREATS - NO WEAPON DISPLAYED                          2
                             VIOLATION OF RESTRAINING ORDER                                  1
                             ASSAULT WITH DEADLY WEAPON, AGGRAVATED ASSAULT                  1
                Southeast    ASSAULT WITH DEADLY WEAPON, AGGRAVATED ASSAULT                  1
                West Valley  CRIMINAL THREATS - NO WEAPON DISPLAYED                          2
                Name: Code Description, dtype: int64
                

                这篇关于使用Pandas GroupBy和VALUE_COUNTS查找最常用的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                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='ZgHSh'></bdo><ul id='ZgHSh'></ul>
                    <legend id='ZgHSh'><style id='ZgHSh'><dir id='ZgHSh'><q id='ZgHSh'></q></dir></style></legend>

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

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

                        <tfoot id='ZgHSh'></tfoot>