我有一个像这样的pandas数据框:
Location Test# Type Parm1 Weight
M36 Test1 A 1.39 233
Test2 B 1.44 281
Test3 B 1.40 239
Test4 A 1.49 438
Test5 C 0.99 112
Test6 C 1.74 200
Test7 A 1.17 100
Test8 A 2.40 7.8
M37 Test1 B 2.91 232
Test2 A 20.2 0
Test3 C 4.88 958
Test4 A 9.46 0
我想计算每个位置的加权平均值,并将其作为额外列添加到数据框中,如下所示:
Location Test# Type Parm1 Weight Weighted Ave.
M36 Test1 A 1.39 233 1.434
Test2 B 1.44 281
Test3 B 1.40 239
Test4 A 1.49 438
Test5 C 0.99 112
Test6 C 1.74 200
Test7 A 1.17 100
Test8 A 2.40 7.8
M37 Test1 B 2.91 232 4.495
Test2 A 20.2 0
Test3 C 4.88 958
Test4 A 9.46 0
答案 0 :(得分:2)
使用groupby
有很多方法可以做到这一点。这应该是比较高效的做法之一。
df.set_index('Location', inplace=True) # set the index
df['Weighted_Sum'] = (df.Parm1 * df.Weight) # calculated weighted sum
v = df[['Weighted_Sum', 'Weight']].sum(level=0) # groupby + sum
df['Weighted Ave'] = v['Weighted_Sum'] / v['Weight'] # calculate the mean
del df['Weighted_Sum'] # drop the surrogate column
df
Test# Type Parm1 Weight Weighted Ave
Location
M36 Test1 A 1.39 233.0 1.434275
M36 Test2 B 1.44 281.0 1.434275
M36 Test3 B 1.40 239.0 1.434275
M36 Test4 A 1.49 438.0 1.434275
M36 Test5 C 0.99 112.0 1.434275
M36 Test6 C 1.74 200.0 1.434275
M36 Test7 A 1.17 100.0 1.434275
M36 Test8 A 2.40 7.8 1.434275
M37 Test1 B 2.91 232.0 4.495933
M37 Test2 A 20.20 0.0 4.495933
M37 Test3 C 4.88 958.0 4.495933
M37 Test4 A 9.46 0.0 4.495933
要获取格式的Weighted Ave
列,请使用mask
-
df['Weighted Ave'] = df['Weighted Ave'].mask(df['Weighted Ave'].duplicated(), '')
答案 1 :(得分:2)
我现在提供另一种使用agg
功能的方式。
基本上,可以使用numpy.average
计算加权平均值,并将df中的Weight
列作为参数提供。
之后,只需使用agg
来聚合此lambda
个函数。您也可以使用apply
。
最后,只需使用join
将加权平均值连接回原始数据框。
df["W_Ave"] = np.NaN
wave = lambda x: np.average(df.loc[x.index, "Parm1"], weights = df.loc[x.index, "Weight"])
f = {"Weighted Average": wave}
df_wave = df.groupby(['Location'])["W_Ave"].agg(wave)
del df["W_Ave"]
dffinal = df.join(df_wave, on="Location")
最后输出:
dffinal
Out[38]:
Location Parm1 Test# Type Weight W_Ave
0 M36 1.39 Test1 A 233.0 1.434275
1 M36 1.44 Test2 B 281.0 1.434275
2 M36 1.40 Test3 B 239.0 1.434275
3 M36 1.49 Test4 A 438.0 1.434275
4 M36 0.99 Test5 C 112.0 1.434275
5 M36 1.74 Test6 C 200.0 1.434275
6 M36 1.17 Test7 A 100.0 1.434275
7 M36 2.40 Test8 A 7.8 1.434275
8 M37 2.91 Test1 B 232.0 4.495933
9 M37 20.20 Test2 A 0.0 4.495933
10 M37 4.88 Test3 C 958.0 4.495933
11 M37 9.46 Test4 A 0.0 4.495933
如果您只对加权平均值感兴趣:
df_wave
Out[39]:
Location
M36 1.434275
M37 4.495933
Name: W_Ave, dtype: float64