2
有没有简单的方法来计算一个熊猫DataFrame中的每一列的平均值,并且对于每一行排除的具体值?下面各行中的x
标记要排除在计算在每次迭代中的值:Pandas DataFrame中每一行的平均函数排除值
a b a b a b
0 1 2 0 x x 0 1 2
1 2 4 first loop 1 2 4 second loop 1 x x etc.
2 3 6 ---> 2 3 6 ---> 2 3 6 --->
3 4 8 3 4 8 3 4 8
4 5 10 4 5 10 4 5 10
____________ _____________
col_avg: 3.5 7.0 col_avg: 3.25 6.5
Using only 4 values at each iteration, as the "x" is excluded from data set
造成了新的数据帧
a_x b_x
0 3.5 7.0
1 3.25 6.5
2 3.0 6.0
3 2.75 5.5
4 2.5 5.0
由于
/N
谢谢,完美的作品! 由于数据包含NaN值,因此需要使用df.count()而不是df_shape [0]计算“真”平均值,否则我会将NaN计为实际值。然而,我没有在问题中具体说明,而只是供参考。 – gussilago