我有一个分组依据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)
计算得出,等
如何使用新代码或修改上面的代码来做到这一点?谢谢。
答案 0 :(得分:1)
添加normalize
df.loc[df['state'].isin(['Alabama','Arizona'])].groupby(df['state'])['industry'].value_counts(sort = True, normalize=True)