为什么在这种情况下 pandas 中的 .mean() 方法的坐标轴是相反的?
Why is the axes for the .mean() method in pandas the opposite in this scenario?
我有一个数据框 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 中的 axis 参数应该设置为 0,因为这对应于跨行应用平均值,但是 axis=1 是我正在寻找的结果。为什么是这样?如果 axis=1
用于列而 axis=0
用于行,那么为什么 .mean(axis=1)
对行取平均值?
只需要告诉 mean 跨列工作 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
我有一个数据框 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 中的 axis 参数应该设置为 0,因为这对应于跨行应用平均值,但是 axis=1 是我正在寻找的结果。为什么是这样?如果 axis=1
用于列而 axis=0
用于行,那么为什么 .mean(axis=1)
对行取平均值?
只需要告诉 mean 跨列工作 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