Divide Column in Pandas Dataframe by Sum of Column

13,159

Solution 1

df['new'] = df['ColA'] /  df['ColA'].sum()

should work

Solution 2

Another approach is to use transform:

df['New Col'] = df['ColA'].transform(lambda x: x / x.sum())

Solution 3

You are very close. You want to perform the sum() on the Col A series

df['New Col'] = df['Col A']/df['Col A'].sum()

Results in a dataframe that looks like this:

>>> df
   Col A   New Col
0      2  0.222222
1      3  0.333333
2      4  0.444444

Now if you do df.sum() you get a Series with the totals per column:

>>> df.sum()
Col A      9.0
New Col    1.0
dtype: float64
Share:
13,159

Related videos on Youtube

spacedinosaur10
Author by

spacedinosaur10

Updated on June 04, 2022

Comments

  • spacedinosaur10
    spacedinosaur10 almost 2 years

    I have a dataframe where I would like to divide each row within column A by the sum of column A and make that a new column within the dataframe.

    Example:
    
            Col A   New Col
            2       .22
            3       .33
            4       .44
    Total = 9       1.00
    

    I tried to sum Col A and then tried to divide by 'Total' but because Total is not a column but a row, it did not work. I just get NaN for each row within the new column.

    df['New Col']= (df['ColA']/df.loc['Total']) 
    

    I know you can also probably integrate a sum calculation within the one line of code instead of creating a totals row as well but not sure how to do that and could not find anything online.

    df['New Col']= (df['ColA']/df.sum()) 
    

    Ideas?