Home > OS >  Quickly replace values in a Pandas DataFrame
Quickly replace values in a Pandas DataFrame

Time:10-09

I have the following dataframe:

df = pd.DataFrame(
    {
        'A':[1,2],
        'B':[3,4]
    }, index=['1','2'])

df.loc[:,'Sum'] = df.sum(axis=1)
df.loc['Sum'] = df.sum(axis=0)

print(df)

#      A  B  Sum
# 1    1  3    4
# 2    2  4    6
# Sum  3  7   10

I want to:

  • replace 1 by 3*4/10
  • replace 2 by 3*6/10
  • replace 3 by 4*7/10
  • replace 4 by 7*6/10

What is the easiest way to do this? I want the solution to be able to extend to n number of rows and columns. Been cracking my head over this. TIA!

CodePudding user response:

If I understood you correctly:

df = pd.DataFrame(
    {
        'A':[1,2],
        'B':[3,4]
    }, index=['1','2'])

df.loc[:,'Sum'] = df.sum(axis=1)
df.loc['Sum'] = df.sum(axis=0)
print(df)

conditions = [(df==1), (df==2), (df==3), (df==4)]
values = [(3*4)/10, (3*6)/10, (4*7)/10, (7*6)/10]
df[df.columns] = np.select(conditions, values, df)

OutPut:

       A    B   Sum
1    1.2  2.8   4.2
2    1.8  4.2   6.0
Sum  2.8  7.0  10.0

CodePudding user response:

Let us try create it from original df before you do the sum and assign

import numpy as np 

v = np.multiply.outer(df.sum(1).values,df.sum().values)/df.sum().sum()
out = pd.DataFrame(v,index=df.index,columns=df.columns)
out
Out[20]: 
     A    B
1  1.2  2.8
2  1.8  4.2
  • Related