efficient function to find harmonic mean across different pandas dataframes
<p>I have several dataframes with identical shape/types, but slightly different numeric values.  I can easily produce a new dataframe with the mean of all input dataframes via:</p>

<pre><code>df = pd.concat([input_dataframes])
df = df.groupby(df.index).mean()
</code></pre>

<p>I want to do the same with harmonic mean (probably the scipy.stats.hmean function).  I have attempted to do this using:</p>

<pre><code>.groupby(df.index).apply(scipy.stats.hmean)
</code></pre>

<p>But this alters the structure of the dataframe.  Is there a better way to do this, or do I need to use a more lengthly/manual implementation?</p>

<p>To illustrate :</p>

<pre><code>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
</code></pre>

<p>Create a pandas Panel, and apply the harmonic mean function over the 'item' axis.</p>

<p>Example with your dataframes <code>df1</code> and <code>df2</code>:</p>

<pre><code>import pandas as pd
from scipy import stats

d = {'1':df1,'2':df2}
pan = pd.Panel(d)
pan.apply(axis='items',func=stats.hmean)
</code></pre>

<p>yields:</p>

<pre><code>        'a'         'b'         'c'
'x'     1.333333    1.333333    2.666667
'y'     2.400000    2.400000    4.800000
'z'     3.428571    3.428571    6.857143
</code></pre>

python pandas scipy