I have table imported into python using pd.read_csv that looks as follows
I need to perform 3 activities on this table as follows
- Calculate the number of free apps and paid apps in each genre using group by.
I wrote following code to get the desired output `
df.groupby(['prime_genre','Subscription'])[['id']].count()`
Output:
- Convert the result from 1 into a dataframe having the columns as prime_genre, free, paid and the rows having the count of each
I wrote the following code to get the desired output
df1 = df.groupby(['prime_genre','Subscription'])['id'].count().reset_index() df1.pivot_table(index='prime_genre', columns='Subscription', values='id', aggfunc='sum')
Output:
Now I need to initialize a column 'Total' that captures the sum of 'free app' and 'paid app' within the pivot table itself
I also I need to initialize two more columns perc_free and perc_paid that displays the percentage of free apps and paid apps within the pivot table itself
How do I go about 3 & 4?