i have several dataframes identical shape/types, different numeric values. can produce new dataframe mean of input dataframes via:
df = pd.concat([input_dataframes]) df = df.groupby(df.index).mean()
i want same harmonic mean (probably scipy.stats.hmean function). have attempted using:
.groupby(df.index).apply(scipy.stats.hmean)
but alters structure of dataframe. there better way this, or need use more lengthly/manual implementation?
to illustrate :
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
create pandas panel, , apply harmonic mean function on 'item' axis.
example dataframes df1
, df2
:
import pandas pd scipy import stats d = {'1':df1,'2':df2} pan = pd.panel(d) pan.apply(axis='items',func=stats.hmean)
yields:
'a' 'b' 'c' 'x' 1.333333 1.333333 2.666667 'y' 2.400000 2.400000 4.800000 'z' 3.428571 3.428571 6.857143
Comments
Post a Comment