Search code examples
pythonpandascalculated-columnscomposite

Creating new pandas column from weighted values in other columns


I'm trying to create a new column by totalling the weighted values in earlier columns.

example

 v1    v2   v3  
  1    2    3
  4    3    6
  3    2    1

weights = [0.801,0.796,0.637] 

I would like to multiply v1 by weights[0], then v2 by weights[1] etc and create a new column that is the total of these operations

so that the output would be

 v1       v2      v3    v_tot 
0.801   1.592   -1.911  0.482
3.204   2.388   -3.822  1.77
2.403   1.592   -0.637  3.358

I have tried reating a new df

data2 = data3.mul(weights_a)

and this works, but when I try to create a new column

data2['v_tot'] = data2.loc[:,:].sum()

the new column is created but it's full of nan values.

can anyone offer some advice?


Solution

  • s = """
     v1    v2   v3  
      1    2    3
      4    3    6
      3    2    1
    """
    df = pd.read_csv(pd.compat.StringIO(s), delim_whitespace=True)
    
    weights = [0.801,0.796,0.637]
    
    df['v_tot'] = (df * weights).sum(axis=1)
    df
    

    Output:

       v1  v2  v3  v_tot
    0   1   2   3  4.304
    1   4   3   6  9.414
    2   3   2   1  4.632
    

    P.S. And if you want to have a DataFrame with weighted values as well:

    dfw = df * weights
    dfw['v_tot'] = dfw.sum(axis=1)
    dfw
    

    Output:

          v1     v2     v3  v_tot
    0  0.801  1.592  1.911  4.304
    1  3.204  2.388  3.822  9.414
    2  2.403  1.592  0.637  4.632