Reputation: 21961
In the foll. dataframe:
Potential Area weight
NAME
Ket 21.694198 61730.10 0.028773
Chles 31.023133 101288.07 0.047211
Caline 23.066401 68515.47 0.031936
Algany 22.794050 109111.05 0.050858
Baire 3.176830 20763.54 0.009678
How can I add a new row that for each column contains the sum of the product of that column with the weight
column? I tried this:
df.loc['new_row'] = df.mean() * df['weight']
Upvotes: 1
Views: 62
Reputation: 164623
Here's one way using NumPy:
# calculate sum of products
sums = (df.iloc[:, :2].values * df['weight'].values[:, None]).sum(0).tolist()
# assign to index label
df.loc['MeanWeightProd'] = sums + [np.nan]
print(df)
Potential Area weight
NAME
Ket 21.694198 61730.100000 0.028773
Chles 31.023133 101288.070000 0.047211
Caline 23.066401 68515.470000 0.031936
Algany 22.794050 109111.050000 0.050858
Baire 3.176830 20763.540000 0.009678
MeanWeightProd 4.015494 14496.300611 NaN
Upvotes: 1