user2293224
user2293224

Reputation: 2220

Python- Convert groupby return value into desired data frame

I am trying to convert data frame A

Dataframe A

App_ID  Review_Rating   Grouping
 4        3              Low
13        2              Low
13        2              Low
20        2              Low
20        4              High
8         1              Low
8         1              Low
8         1              Low
15        4              High
7         3              Low
4         3              Low
4         3              Low
4         3              Low
4         3              Low
4         5              High
7         5              High
15        3              Low

into Dataframe B (which contains grouping value into percentage):

App_ID    Percentage of Grouping
          Low     High
4         75      25
13        100     0
20        33      67
8         100     0
15        42.8    57.14
7         37.5    62.5

Here is my code:

dataframeB=(dataframeA.groupby(['App_ID','Grouping'])['Review_Rating'].count()/dataframeA.groupby('App_ID')['Review_Rating'].count()*100).reset_index()

However it does not give desired output. Output of my code:

App_ID   Grouping   Review_Rating
4        Low        75
4        High       25
13       Low        100
13       High       0
20       Low        33
20       High       67
8        Low        100
8        High       0
15       Low        42.8
15       High       57.14
7        Low        37.5
7        High       62.5

Looking for your help to rectify the issue. Thanks in advance

Upvotes: 2

Views: 38

Answers (1)

BENY
BENY

Reputation: 323226

Very close to pivot problem , but you need normalized . Using crosstab

pd.crosstab(df.App_ID,df.Grouping,df.Review_Rating,aggfunc='sum',normalize ='index')*100
Out[90]: 
Grouping       High         Low
App_ID                         
4         25.000000   75.000000
7         62.500000   37.500000
8          0.000000  100.000000
13         0.000000  100.000000
15        57.142857   42.857143
20        66.666667   33.333333

Upvotes: 2

Related Questions