Panda Python - Split a column by 100 (then round by 2.dp)

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

I just want to split the percent column in the format "61.72" by 100, then round it to 2.dp to fit the dataframe.

Is there an easy way to do this?

There are two columns in my dataframe, one is called "A" and the other is called "B", I want to format "B".

Many thanks!

+3


source to share


1 answer


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

      

+5


source







All Articles