pandas :从行中的每个元素中减去行均值 [英] Pandas: Subtract row mean from each element in row

查看:91
本文介绍了 pandas :从行中的每个元素中减去行均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,其中的行按化学元素类型索引,而列则代表不同的样品.这些值是浮点数,表示每个样本中行元素的存在程度.

I have a dataframe with rows indexed by chemical element type and columns representing different samples. The values are floats representing the degree of presence of the row element in each sample.

我想计算每一行的平均值,并从该特定行中的每个值中减去它,以对数据进行归一化,并为该数据集创建一个新的数据框.

I want to compute the mean of each row and subtract it from each value in that specific row to normalize the data, and make a new dataframe of that dataset.

我尝试使用mean(1),它为我提供了一个具有每个化学元素均值的Series对象,这很好,但是后来我尝试使用减法,但这没用.

I tried using mean(1), which give me a Series object with the mean for each chemical element, which is good, but then I tried using subtract, which didn't work.

推荐答案

您可以使用DataFrame的sub方法,并指定相减应按行(axis=0)进行,而不是按列进行默认操作:

You could use DataFrame's sub method and specify that the subtraction should happen row-wise (axis=0) as opposed to the default column-wise behaviour:

df.sub(df.mean(axis=1), axis=0)

这是一个例子:

>>> df = pd.DataFrame({'a': [1.5, 2.5], 'b': [0.25, 2.75], 'c': [1.25, 0.75]})
>>> df
     a     b     c
0  1.5  0.25  1.25
1  2.5  2.75  0.75

每行的均值很容易计算:

The mean of each row is straightforward to calculate:

>>> df.mean(axis=1)
0    1
1    2
dtype: float64

要取消对DataFrame行的平均,只需像这样从df中减去行的平均值:

To de-mean the rows of the DataFrame, just subtract the mean values of rows from df like this:

>>> df.sub(df.mean(axis=1), axis=0)
     a     b     c
0  0.5 -0.75  0.25
1  0.5  0.75 -1.25

这篇关于 pandas :从行中的每个元素中减去行均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆