您可以使用Dataframe的
sub方法并指定相减应按行(
axis=0)进行,而不是按默认的按列行为:
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 c0 1.5 0.25 1.251 2.5 2.75 0.75每行的平均值很容易计算:
>>> df.mean(axis=1)0 11 2dtype: float64
要取消对Dataframe的行的平均,只需从中减去行的平均值,
df如下所示:
>>> df.sub(df.mean(axis=1), axis=0) a b c0 0.5 -0.75 0.251 0.5 0.75 -1.25



