I have a pandas groupby that I've done
grouped = df.groupby(['name','type'])['count'].count().reset_index()
Looks like this:
name type count
x a 32
x b 1111
x c 4214
What I need to do is take this and generate percentages, so i would get something like this (I realize the percentages are incorrect):
name type count
x a 1%
x b 49%
x c 50%
I can think of some pseudocode that might make sense but I haven't been able to get anything that actually works...
something like
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)
Is there a good way to do this with pandas?