Add column for total to Pandas dataframe

I have a dataframe which I am doing groupby()

on to get a count of the column values. I am trying to add an extra "Percentage of Total" column. I do not know how to do that.

I've looked at several options for groupby, but can't find anything that works.

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

      

+3


source to share


1 answer


Option 1

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

      



Option 2
Use pd.value_counts

insteadgroupby

pre_df.DAYSLATE.value_counts(normalize=True)

      

+7


source







All Articles