Reputation: 3751
consider below pandas
DataFrame
df = pd.DataFrame(np.random.randint(1,10,15).reshape(-1,3), columns = list('abc'))
i want to update values of all the three columns as ratios in place (ie columns values divided by sum of all three columns, axis = 1) in a single instance.
something like:
df = df.loc[:,['a','b','c']] / df.loc[:,['a','b','c']].sum(axis = 1)
how can i achieve this?
Upvotes: 0
Views: 456
Reputation: 214967
You can use pandas.DataFrame.div method:
df.div(df.sum())
# a b c
#0 0.071429 0.368421 0.173913
#1 0.214286 0.210526 0.391304
#2 0.214286 0.052632 0.043478
#3 0.285714 0.105263 0.130435
#4 0.214286 0.263158 0.260870
To divide by rows, specify axis accordingly:
df1 = df.div(df.sum(axis = 1), axis = 0)
Here is a test that the result data frame has rowsum of one.
df1.sum(axis = 1)
#0 1.0
#1 1.0
#2 1.0
#3 1.0
#4 1.0
#dtype: float64
Upvotes: 3