Python:如何在数据帧中获得多个数组的元素标准偏差

时间:2017-09-18 11:26:33

标签: python pandas numpy standard-deviation elementwise-operations

我有一个相当大的数据帧(df),每个单元格中包含数组和NaN,前3行如下所示:

df:
                 A                B                C
X  [4, 8, 1, 1, 9]              NaN  [8, 2, 8, 4, 9]
Y  [4, 3, 4, 1, 5]  [1, 2, 6, 2, 7]  [7, 1, 1, 7, 8]
Z              NaN  [9, 3, 8, 7, 7]  [2, 6, 3, 1, 9]

我已经知道(thanks to piRSquared)如何对每列的行采用元素方式的意思,以便我得到这个:

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]

现在我想知道如何获得各自的标准偏差,任何想法?另外,我还不了解groupby()正在做什么,有人可以更详细地解释它的功能吗?

df

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

element_wise_mean

df2               = df.stack().groupby(level=1)
element_wise_mean = df2.apply(np.mean, axis=0)

element_wise_sd

element_wise_sd   = df2.apply(np.std, axis=0)
TypeError: setting an array element with a sequence.

2 个答案:

答案 0 :(得分:3)

使用lambda转换为numpy array时使用np.std对我有用:

element_wise_std = df2.apply(lambda x: np.std(np.array(x), 0))
#axis=0 is by default, so can be omit
#element_wise_std = df2.apply(lambda x: np.std(np.array(x)))
print (element_wise_std)
A                            [0.0, 2.5, 1.5, 0.0, 2.0]
B                            [4.0, 0.5, 1.0, 2.5, 0.0]
C    [2.62466929134, 2.16024689947, 2.94392028878, ...
dtype: object

来自comment的解决方案:

element_wise_std = df2.apply(lambda x: np.std(x.values, 0))
print (element_wise_std)
A                            [0.0, 2.5, 1.5, 0.0, 2.0]
B                            [4.0, 0.5, 1.0, 2.5, 0.0]
C    [2.62466929134, 2.16024689947, 2.94392028878, ...
dtype: object

我尝试解释更多:

首先按stack重新塑造 - 将列添加到index并创建Multiindex

print (df.stack())
X  A    [4, 8, 1, 1, 9]
   C    [8, 2, 8, 4, 9]
Y  A    [4, 3, 4, 1, 5]
   B    [1, 2, 6, 2, 7]
   C    [7, 1, 1, 7, 8]
Z  B    [9, 3, 8, 7, 7]
   C    [2, 6, 3, 1, 9]
dtype: object

然后groupby(level=1)表示按Multiindex的第一级分组 - (按值ABC)和apply某些功能。这是np.std

Pandas不使用array或列表非常好,所以转换是必要的。 (看起来像虫子)

答案 1 :(得分:2)

Jezrael打败了我:

要回答有关.groupby()的问题,请尝试.apply(print)。您将看到返回的内容,并在apply函数中使用:

df2 = df.stack().groupby(axis=1) #groups by the second index of df.stack()
df2.apply(print)
X  A    [4, 8, 1, 1, 9]
Y  A    [4, 3, 4, 1, 5]
Name: A, dtype: object
Y  B    [1, 2, 6, 2, 7]
Z  B    [9, 3, 8, 7, 7]
Name: B, dtype: object
X  C    [8, 2, 8, 4, 9]
Y  C    [7, 1, 1, 7, 8]
Z  C    [2, 6, 3, 1, 9]
Name: C, dtype: object

相反,请尝试:

df3 = df.stack().groupby(level=0) #this will group by the first index of df.stack()
df3.apply(print)
X  A    [4, 8, 1, 1, 9]
   C    [8, 2, 8, 4, 9]
Name: X, dtype: object
Y  A    [4, 3, 4, 1, 5]
   B    [1, 2, 6, 2, 7]
   C    [7, 1, 1, 7, 8]
Name: Y, dtype: object
Z  B    [9, 3, 8, 7, 7]
   C    [2, 6, 3, 1, 9]
Name: Z, dtype: object