小编Bry*_*ipp的帖子

在不同的熊猫数据帧之间找到调和平均值的有效函数

我有几个具有相同形状/类型但数值略有不同的数据框。我可以通过以下方式轻松生成具有所有输入数据帧平均值的新数据帧:

df = pd.concat([input_dataframes])
df = df.groupby(df.index).mean()
Run Code Online (Sandbox Code Playgroud)

我想对调和平均值(可能是 scipy.stats.hmean 函数)做同样的事情。我尝试使用以下方法执行此操作:

.groupby(df.index).apply(scipy.stats.hmean)
Run Code Online (Sandbox Code Playgroud)

但这会改变数据帧的结构。有没有更好的方法来做到这一点,或者我是否需要使用更长的/手动实现?

为了显示 :

df_input1:
   'a' 'b' 'c'
'x' 1   1   2 
'y' 2   2   4 
'z' 3   3   6

df_input2:
   'a' 'b' 'c'
'x' 2   2   4 
'y' 3   3   6 
'z' 4   4   8

desired output (but w/ hmean):
   'a'  'b'  'c'
'x' 1.5  1.5  3 
'y' 2.5  2.5  5 
'z' 3.5  3.5  7
Run Code Online (Sandbox Code Playgroud)

python scipy pandas

4
推荐指数
1
解决办法
2073
查看次数

标签 统计

pandas ×1

python ×1

scipy ×1