Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在数据帧中取两个值之间的平均值_Python_Pandas_Dataframe_Loops - Fatal编程技术网

Python 在数据帧中取两个值之间的平均值

Python 在数据帧中取两个值之间的平均值,python,pandas,dataframe,loops,Python,Pandas,Dataframe,Loops,假设我有以下数据帧 A B C D E 0 1.625627 8.910396 9.171640 1.980580 8.429633 1 7.228290 6.431085 5.399684 8.442247 2.609367 2 NaN NaN NaN NaN NaN 3 2.533768 3.877104 8.199575 5.13

假设我有以下数据帧

A   B   C   D   E
0   1.625627    8.910396    9.171640    1.980580    8.429633
1   7.228290    6.431085    5.399684    8.442247    2.609367
2   NaN         NaN         NaN         NaN         NaN
3   2.533768    3.877104    8.199575    5.138173    7.248905
4   0.351828    1.233081    1.004183    6.497358    0.76487
我想迭代每一行,将NaN值替换为上限值和下限值之间的平均值

我尝试使用以下代码,但没有任何结果:

for i, row in df.iterrows():
    if i in row[:] > 1.0:
        print(i)
使用
fillna()
shift()
<代码>移位(1)将为您提供上限值(当其向下移位数据帧时),而
移位(-1)
将为您提供下限值(当其向上移位数据帧时)


无需遍历列,pandas将根据列标题自动对齐。
df.fillna((df.shift(-1)+df.shift())/2)
Ohh是,编辑答案:)
df = df.fillna((df.shift(1)+df.shift(-1))/2)
df.fillna((df.shift(1)+df.shift(-1))/2, inplace = True)