是的,我已经看到许多其他问题,这些问题被问到如何计算逐行平均值,并且已经用平凡的数据帧重现了这些方法。但是这些方法都不适用于我的大型数据集。而是.mean(axis = 1)仅返回最左边一列的值。
In [72]: pd.__version__
Out[72]: '1.0.3'
In [73]: df
Out[73]:
Constituent Julian_day Depth ... Seg_31 Elevation.5 mean
0 Temperature 37622.5 -0.585 ... 7.32 NaN 7.40
1 Temperature 37622.5 -1.670 ... 7.32 NaN 7.40
2 Temperature 37622.5 -2.670 ... 7.32 NaN 7.40
3 Temperature 37622.5 -3.670 ... 7.32 NaN 7.40
4 Temperature 37622.5 -4.670 ... 7.32 NaN 7.40
... ... ... ... ... ... ... ...
87195 pH_ 37986.5 -87.613 ... NaN NaN 6.77
87196 pH_ 37986.5 -90.613 ... NaN NaN 6.77
87197 pH_ 37986.5 -93.613 ... NaN NaN 6.75
87198 pH_ 37986.5 -96.613 ... NaN NaN 6.73
87199 pH_ 37986.5 -99.613 ... NaN NaN 6.71
[87200 rows x 15 columns]
In [74]: df[segs]
Out[74]:
Seg_11 Seg_24 Seg_25 Seg_26 Seg_31
0 7.40 7.49 7.48 7.48 7.32
1 7.40 7.49 7.48 7.48 7.32
2 7.40 7.49 7.48 7.48 7.32
3 7.40 7.49 7.48 7.48 7.32
4 7.40 7.49 7.48 7.48 7.32
... ... ... ... ... ...
87195 6.77 NaN NaN NaN NaN
87196 6.77 NaN NaN NaN NaN
87197 6.75 NaN NaN NaN NaN
87198 6.73 NaN NaN NaN NaN
87199 6.71 NaN NaN NaN NaN
[87200 rows x 5 columns]
In [75]: df[segs].mean(axis=1)
Out[75]:
0 7.40
1 7.40
2 7.40
3 7.40
4 7.40
...
87195 6.77
87196 6.77
87197 6.75
87198 6.73
87199 6.71
Length: 87200, dtype: float64
segs is an array of the column names. I get the same result if I use df.loc[:,segs]
所有行似乎都存在此问题,但是作为对显示的前五个的检查,(7.40 + 7.49 + 7.48 + 7.48 + 7.32)/ 5 == 7.43。
我试图在测试数据框中插入一两个NaN,但这不足以重现该问题。这里发生了什么?