如何在Python中计算groupby中的计数和百分比

时间:2016-10-06 10:06:38

标签: python pandas group-by

分组后我有以下输出
Publisher.groupby('Category')['Title'].count()
Category
Coding          5
Hacking         7
Java            1
JavaScript      5
LEGO           43
Linux           7
Networking      5
Others        123
Python          8
R               2
Ruby            4
Scripting       4 
Statistics      2
Web             3

在上面的输出中,我想要百分比,即第一行5*100/219,依此类推。我正在做以下

 Publisher.groupby('Category')['Title'].agg({'Count':'count','Percentage':lambda x:x/x.sum()})

但它给了我一个错误。请帮忙

2 个答案:

答案 0 :(得分:3)

我认为你可以使用:

P = Publisher.groupby('Category')['Title'].count().reset_index()
P['Percentage'] = 100 * P['Title']  / P['Title'].sum()

样品:

Publisher = pd.DataFrame({'Category':['a','a','s'],
                   'Title':[4,5,6]})

print (Publisher)
  Category  Title
0        a      4
1        a      5
2        s      6

P = Publisher.groupby('Category')['Title'].count().reset_index()
P['Percentage'] = 100 * P['Title']  / P['Title'].sum()
print (P)
  Category  Title  Percentage
0        a      2   66.666667
1        s      1   33.333333

答案 1 :(得分:0)

df = pd.DataFrame({'Category':['a','a','s'],
                   'Title':[4,5,6]})

df=df.groupby('Category')['Title'].count().rename("percentage").transform(lambda x: x/x.sum())

df.reset_index()

#output in dataframe type

    Category    percentage
0   a   0.666667
1   s   0.333333

#please let me know if it doesn't solve your current problem