pandas Dataframe divide a column with a specific value and create new column with the result?

                            A                            B
0                          0.119                      5.344960e+08  
1                          0.008                      7.950629e+09  
2                        318.575                      1.996548e+05  
3                        153.644                      4.139767e+05  

    sum = 63605028.818
    df['B'] = df['A'].rdiv(sum).replace(np.inf, 0).round(3)

Getting exponential values(as a series) , I want normal numerical values in B column like - 534496040.49 etc.


Solution 1:

You can do something like this:

df['B'] = df['A'].rdiv(my_sum).replace(np.inf, 0).astype('int64')

You can also change the view option of pandas:

pd.set_option('display.float_format', lambda x: '%.3f' % x)

Solution 2:

import pandas as pd
pd.options.display.float_format = '{:,.3f}'.format

Set float_format option/setting of pandas and it will show all floats in this format. You won't need to explicitly round each column.

Alternatively, use map()

df['B'] = df['A'].rdiv(sum).replace(np.inf, 0)
df['B'] = df['B'].map(':,.3f'.format)