简体   繁体   English

在熊猫数据框中替换nan

[英]replace nan in pandas dataframe

given the dataframe df 给定数据帧df

df = pd.DataFrame(data=[[np.nan,1],
                  [np.nan,np.nan],
                  [1,2],
                  [2,3],
                  [np.nan,np.nan],
                  [np.nan,np.nan],
                  [3,4],
                  [4,5],
                  [np.nan,np.nan],
                  [np.nan,np.nan]],columns=['A','B'])


df
Out[16]: 
     A    B
0  NaN  1.0
1  NaN  NaN
2  1.0  2.0
3  2.0  3.0
4  NaN  NaN
5  NaN  NaN
6  3.0  4.0
7  4.0  5.0
8  NaN  NaN
9  NaN  NaN

I would need to replace the nan using the following rules: 我需要使用以下规则替换nan

1) if nan is at the beginning replace with the first values after the nan 1)如果nan在开头,则用nan之后的第一个值替换

2) if nan is in the middle of 2 or more values replace the nan with the average of these values 2)如果nan在2个或多个值的中间,则用这些值的平均值替换nan

3) if nan is at the end replace with the last value 3)如果nan在最后,则替换为最后一个值

df
Out[16]: 
     A    B
0  1.0  1.0
1  1.0  1.5
2  1.0  2.0
3  2.0  3.0
4  2.5  3.5
5  2.5  3.5
6  3.0  4.0
7  4.0  5.0
8  4.0  5.0
9  4.0  5.0

Use add between forward filling and backfilling values, then divide by 2 and last replace last and first NaN s: 在正向填充和回填值之间使用add ,然后除以2 ,最后替换last和first NaN

df = df.bfill().add(df.ffill()).div(2).ffill().bfill()
print (df)
     A    B
0  1.0  1.0
1  1.0  1.5
2  1.0  2.0
3  2.0  3.0
4  2.5  3.5
5  2.5  3.5
6  3.0  4.0
7  4.0  5.0
8  4.0  5.0
9  4.0  5.0

Detail : 详细说明

print (df.bfill().add(df.ffill()))

     A     B
0  NaN   2.0
1  NaN   3.0
2  2.0   4.0
3  4.0   6.0
4  5.0   7.0
5  5.0   7.0
6  6.0   8.0
7  8.0  10.0
8  NaN   NaN
9  NaN   NaN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM