首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Groupby、value counts和calculate percentage in Pandas

Groupby、value counts和calculate percentage in Pandas
EN

Stack Overflow用户
提问于 2020-06-03 09:36:03
回答 1查看 130关注 0票数 1

我有groupby state,value计算数据帧的industry

代码语言:javascript
复制
df.loc[df['state'].isin(['Alabama','Arizona'])].groupby(df['state'])['industry'].value_counts(sort = True)

输出:

代码语言:javascript
复制
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,我想知道由224/ (224 + 7 + ... + 1)计算的Financial Services的百分比,等等。

如何通过使用新代码或修改上面的代码来做到这一点?谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-03 09:37:40

添加normalize

代码语言:javascript
复制
df.loc[df['state'].isin(['Alabama','Arizona'])].groupby(df['state'])['industry'].value_counts(sort = True, normalize=True)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62163880

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档