From SO:
np.random.seed([3,14159])
df = pd.DataFrame(
np.random.randint(10, size=(3, 3, 5)).tolist(),
list('XYZ'), list('ABC')
).applymap(np.array)
df.loc['X', 'B'] = np.nan
df.loc['Z', 'A'] = np.nan
print (df)
df2 = df.stack().groupby(level=1)
element_wise_mean = df2.apply(np.mean, axis=0)
print (element_wise_mean)
A [4.0, 5.5, 2.5, 1.0, 7.0]
B [5.0, 2.5, 7.0, 4.5, 7.0]
C [5.66666666667, 3.0, 4.0, 4.0, 8.66666666667]
element_wise_sd = df2.apply(np.std, axis=0)
print (element_wise_sd)
TypeError: setting an array element with a sequence.
I try find solution and if cast to numpy array it working nice.
I know pandas not support arrays using this way very nice, but maybe it is simple bug.
Thanks.
Comment From: jreback
In [13]: df.stack().groupby(level=0).apply(lambda x: np.std(x.tolist()))
Out[13]:
X 3.168596
Y 2.489087
Z 2.837252
dtype: float64
you need to stress in SO answers that this is a completely non-idiomatic way of doing things, and pandas does not support this, aside from being completely non-performant.