【发布时间】:2019-05-21 11:57:06
【问题描述】:
我已经完成了一个 pandas groupby
grouped = df.groupby(['name','type'])['count'].count().reset_index()
看起来像这样:
name type count
x a 32
x b 1111
x c 4214
我需要做的是获取这个并生成百分比,所以我会得到这样的东西(我意识到百分比不正确):
name type count
x a 1%
x b 49%
x c 50%
我能想到一些可能有意义的伪代码,但我无法得到任何真正有效的东西......
类似
def getPercentage(df):
for name in df:
total = 0
where df['name'] = name:
total = total + df['count']
type_percent = (df['type'] / total) * 100
return type_percent
df.apply(getPercentage)
有没有用熊猫做这件事的好方法?
【问题讨论】:
-
你能提供一个简短的输入样本和你期望给定样本的输出吗?
标签: python pandas percentage