Home > database >  Perform multiple math operations on columns in df
Perform multiple math operations on columns in df

Time:12-08

I want to do operation [(b-a)/a] * 100 on a dataframe [i.e., percentage change from a reference value]. where a is my first column and b is all other columns of the dataframe. I tried below steps and it is working but very messy !!

df = pd.DataFrame({'obj1': [1, 3, 4],
                   'obj2': [6, 9, 10], 'obj3':[2, 6, 8]},
                  index=['circle', 'triangle', 'rectangle'])

#first we subtract all columns with first col - as that is the starting point : b-a

df_aftersub = df.sub(pd.Series(df.iloc[:,[0]].squeeze()),axis='index')

#second we divide the result with first column to get change - b-a/a

df_change = df_aftersub.div(pd.Series(df.iloc[:,[0]].squeeze()),axis='index')

#third we multiply with 100 to get percent change - b-a/a*100

df_final = df_change*100

df_final

output needed

            obj1 obj2   obj3
circle      0.0 500.0   100.0
triangle    0.0 200.0   100.0
rectangle   0.0 150.0   100.0

how to do it in less lines of code and if possible less temporary dataframes (if possible simple to understand)

CodePudding user response:

First subtract first column by DataFrame.sub and divide by DataFrame.div, last multiple by 100:

s = df.iloc[:, 0]
df_final = df.sub(s, axis=0).div(s, axis=0).mul(100)
print (df_final)
           obj1   obj2   obj3
circle      0.0  500.0  100.0
triangle    0.0  200.0  100.0
rectangle   0.0  150.0  100.0
  • Related