1

I have the following dataframe:

df= 
      product_name     0     1    2
0       laptop       1200  1000  100
1      printer       150    10  100
2       tablet      300    30  560
3         desk      450    40  640
4        chair      200    20  207

I want to add the sum of column 1 and 2 but with multiplying the column 1 with 0.7 and 2 by 0.3.

I tried to make the sum for these 2 columns like:

df[[1, 2].sum(axis = 1)

I can do like that

df['Value'] = df.apply(lambda cols: cols[1]*0.7+cols[2]*0.3, axis=1)

But I am looking to send the 0.7 and 0.3 as parameter not hard coded.

bib
  • 944
  • 3
  • 15
  • 32

2 Answers2

1

Let's say you have a = 0.7. You can always do

df['Value'] = a * df[1] + (1 - a) * df[2]

Alternatively, you can use numpy for many columns. Say you have weights = [0.5, 0.3, 0.2] as the weights for the numerical columns, and index = [0, 1, 2] to mark which columns correspond to the weights:

df[index].to_numpy().dot(weights)
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
0

I solved this question finally like that:

def weighted_average(data, cols, weights):
    columns = data.columns
    weight = pd.DataFrame(pd.Series(weights, index=columns, name=0))
    weight_index = []
    for name in cols:
        index_no = data.columns.get_loc(name)
        weight_index.append(index_no)
    weight = weight.iloc[weight_index]
    return data[cols].dot(weight)
bib
  • 944
  • 3
  • 15
  • 32