Reputation: 4398
I have a dataset where I would like to display the percent as a result from grouping by 'type' as well as calculate the sum of two specific columns individually.
Data
free use type date
10 5 A1 1/1/2021
20 1 A1 1/1/2021
40 2 B2 1/1/2021
60 3 B2 1/1/2021
Desired
type percent use free date
A1 50 6 30 1/1/2021
B2 50 5 100 1/1/2021
Doing
percent = count/total *100
df.groupby(['type, 'date']).sum()
This gives me all but the percent. I am still researching. Any suggestion is appreciated
Upvotes: 1
Views: 50
Reputation: 1377
You can try this approach -
percent_series = pd.DataFrame(df.type.value_counts())['type']/len(df)*100
Output
B 50.0
A1 50.0
Name: type, dtype: float64
Upvotes: 1
Reputation: 862761
Use Series.value_counts
with normalize
for percentage of original DataFrame
and then use Series.map
for aggregate values:
df1 = df.groupby(['type', 'date'], as_index=False).sum()
df1['perc'] = df1['type'].map(df['type'].value_counts(normalize=True)).mul(100)
print (df1)
type date free use perc
0 A1 1/1/2021 30 6 50.0
1 B2 1/1/2021 100 5 50.0
Upvotes: 2