18

I have a dataframe that I am doing a groupby() on to get the counts on a column's values. I am trying to add an additional column for "Percentage of Total". I'm not sure how to accomplish that.

I've looked at a few groupby options, but can't seem to find anything that fits.

My dataframe looks like this:

              DAYSLATE
DAYSLATE          
-7 days          1
-5 days          2
-3 days          8
-2 days          9
-1 days         45
0 days         589
1 days          33
2 days           8
3 days          16
4 days          14
5 days          16
6 days           2
7 days           6
8 days           2
9 days           2
10 days          1
0

1 Answer 1

36

Option 1

df['DAYSLATE_pct'] = df.DAYSLATE / df.DAYSLATE.sum()

Option 2
Use pd.value_counts instead of groupby

pre_df.DAYSLATE.value_counts(normalize=True)
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.