0

I want calculate division of each cell by sum of each row. Actually there are many column not only A and B.

import pandas as pd
data = pd.DataFrame({'A':[1,2,3,1,2,3,1],
                 'B':[4,5,6,4,5,6,4]]})

sum_row = data.sum(axis=1)

Here is an example of what I expect. enter image description here

rafaelc
  • 57,686
  • 15
  • 58
  • 82
Ghozally
  • 93
  • 1
  • 8

1 Answers1

0

I think this should do the trick

import pandas as pd
data = pd.DataFrame({'A':[1,2,3,1,2,3,1],
                 'B':[4,5,6,4,5,6,4]})

data['sum_row'] = data.sum(axis=1)

for col in list(data.columns.values):
    data[col + ' / Sum_Row'] = [data['A'].iloc[e] / data['sum_row'].iloc[e] for e in range(0, len(data['A']))]

user123
  • 208
  • 2
  • 8