Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Panda Python - dividing a column by 100 (then rounding by 2.dp)

Tags:

python

pandas

I have been manipulating some data frames, but unfortunately I have two percentage columns, one in the format '61.72' and the other '0.62'.

I want to just divide the column with the percentages in the '61.72' format by 100 then round it to 2.dp so it is consistent with the data frame.

Is there an easy way of doing this?

My data frame has two columns, one called 'A' and the other 'B', I want to format 'B'.

Many thanks!

like image 872
ScoutEU Avatar asked Apr 28 '17 07:04

ScoutEU


Video Answer


1 Answers

You can use div with round:

df = pd.DataFrame({'A':[61.75, 10.25], 'B':[0.62, 0.45]})
print (df)
       A     B
0  61.75  0.62
1  10.25  0.45

df['A'] = df['A'].div(100).round(2)
#same as
#df['A'] = (df['A'] / 100).round(2)
print (df)
      A     B
0  0.62  0.62
1  0.10  0.45
like image 169
jezrael Avatar answered Oct 17 '22 01:10

jezrael