【问题标题】:Groupby, value counts and calculate percentage in PandasGroupby,值计数和计算熊猫中的百分比
【发布时间】:2020-09-21 14:56:36
【问题描述】:

我有 groupby state,数据帧的值计数 industry

df.loc[df['state'].isin(['Alabama','Arizona'])].groupby(df['state'])['industry'].value_counts(sort = True)

输出:

state    industry                              
Alabama  Financial Services                        224
         Education                                   7
         Healthcare, Pharmaceuticals, & Biotech      5
         Business Services                           2
         Other                                       2
         Retail                                      2
         Government                                  1
         Manufacturing                               1
         Transportation & Storage                    1
Arizona  Healthcare, Pharmaceuticals, & Biotech     19
         Other                                      13
         Education                                   5
         Retail                                      5
         Transportation & Storage                    5
         Manufacturing                               4
         Travel, Recreation, and Leisure             4
         Consumer Services                           3
         Energy & Utilities                          2
         Financial Services                          2
         Government                                  2
         Business Services                           1
         Computers & Electronics                     1
         Software & Internet                         1
Name: industry, dtype: int64

现在我想更进一步,获取值计数的百分比,例如,对于Alabama,我想知道Financial Services 的百分比,这是由224/ (224 + 7 + ... + 1) 计算得出的等等。

我怎样才能通过使用新代码或修改上面的代码来做到这一点?谢谢。

【问题讨论】:

    标签: python-3.x pandas dataframe


    【解决方案1】:

    添加normalize

    df.loc[df['state'].isin(['Alabama','Arizona'])].groupby(df['state'])['industry'].value_counts(sort = True, normalize=True)
    

    【讨论】:

      猜你喜欢
      • 2021-01-05
      • 2020-04-17
      • 2022-11-21
      • 2017-05-13
      • 2022-06-13
      • 1970-01-01
      • 2015-10-07
      • 2017-02-15
      • 2022-11-19
      相关资源
      最近更新 更多