首页 > 解决方案 > 为什么在这种情况下,pandas 中 .mean() 方法的轴是相反的?

问题描述

我有一个数据框,height_df,具有三个测量值,'height_1','height_2','height_3'。我想创建一个具有所有三个高度平均值的新列。打印输出height_df如下

    height_1  height_2  height_3
0       1.78      1.80      1.80
1       1.70      1.70      1.69
2       1.74      1.75      1.73
3       1.66      1.68      1.67

以下代码有效,但我不明白为什么

height_df['height'] = height_df[['height_1','height_2','height_3']].mean(axis=1)

我实际上想要跨行轴的平均值,即为每一行计算三个高度的平均值。那时我会认为 mean 中的轴参数应该设置为 0,因为这对应于跨行应用平均值,但是 axis=1 是我正在寻找的结果。为什么是这样?如果axis=1是列和axis=0行,那么为什么要跨行.mean(axis=1)取平均值?

标签: pythonpandasmeanaxis

解决方案


只需要告诉意味着跨列工作axis=1

df = pd.DataFrame({"height_1":[1.78,1.7,1.74,1.66],"height_2":[1.8,1.7,1.75,1.68],"height_3":[1.8,1.69,1.73,1.67]})
df = df.assign(height_mean=df.mean(axis=1))
df = df.assign(height_mean=df.loc[:,['height_1','height_2','height_3']].mean(axis=1))
print(df.to_string(index=False))

输出

 height_1  height_2  height_3  height_mean
     1.78      1.80      1.80     1.793333
     1.70      1.70      1.69     1.696667
     1.74      1.75      1.73     1.740000
     1.66      1.68      1.67     1.670000

推荐阅读