I have Pandas Data Frame in Python like below:
IDX | ALL | COL1 | COL2
------------------------
ABC | 100 | 50 | 214
DEF | 250 | 32 | 89
GHI | 120 | 18 | 12
IDX is and index of this Data Frame. And I would like to add new row in this Data Frame which will calculate mathematic formula like:
(value from index DEF - value from index GHI) and result / by value from index DEF
So for example: (250 - 120) / 250 = 0.52 So as a result I need something like below:
IDX | ALL | COL1 | COL2
------------------------
ABC | 100 | 50 | 214
DEF | 250 | 32 | 89
GHI | 120 | 18 | 12
new1 | 0.52| 0.44 | 0.87
because:
(250 - 120) / 250 = 0.52
(32 - 18) / 32 = 0.44
(89 - 12) / 89 = 0.87
I used code like below:
df.loc['new1'] = df.lfoc['DEF'].sub(df.floc['GHI']).div(df.loc['DEF'])
Nevertheless, after using above code (which works) values in my DF changed from int to float and looks like below:
IDX | ALL | COL1 | COL2
------------------------
ABC | 100.00000 | 50.00000 | 214.00000
DEF | 250.00000 | 32.00000 | 89.00000
GHI | 120.00000 | 18.00000 | 12.00000
new1 | 0.52000 | 0.43750 | 0.86516
What can I do so as to acheive DF like below?????:
IDX | ALL | COL1 | COL2
------------------------
ABC | 100 | 50 | 214
DEF | 250 | 32 | 89
GHI | 120 | 18 | 12
new1 | 52.0| 43.7 | 86.5