【发布时间】: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