Home > database >  Is it possible to round while substracting 2 columns of a dataframe?
Is it possible to round while substracting 2 columns of a dataframe?

Time:09-21

I am wondering if it is possible to use round(2) substracting 2 DF column.

DF['D'] = DF['A'] - DF['B']

What is the best way to round this result ?

CodePudding user response:

DF['D'] = (DF['A'] - DF['B']).round(2) #ThePyGuy solution 

#or

DF['D'] = DF['A'] - DF['B']
DF['D'] = DF['D'].round(2)

#or

DF['D'] = [round(a - b, 2) for a, b in zip(DF['A'], DF['B'])] 

#or 
df['D'] = df.eval("A - B").round(2)

  • Related