Pandas replace zero as the nearest average non-zero value

别说谁变了你拦得住时间么 提交于 2021-02-10 07:33:58

问题


I have a dataframe:

 df = pd.DataFrame({'A':[0,0,15,0,0,12,0,0,0,5]})

And I want to replace the 0 value with the nearest non zero value,

For example, the first value is 0, then I find the the nearest non-zero value is 15, so I replace it as 15, then the data becomes:[15,0,15,0,0,12,0,0,0,5],

Then for all the value except first one, I need to find the both side of the nearest non-zero value, and average them. So for the second 0, it would be (15+15)/2; And the third zero would be (15+12)/2

I only know how to replace zero to the nearest value by:

df['A'].replace(to_replace=0, method='ffill')

0     0
1     0
2    15
3    15
4    15
5    12
6    12
7    12
8    12
9     5

But the first two zero value cannot be replaced, and this way is not getting the average value.


回答1:


While not exactly the same, it seems like a good solution to your problem would be to apply a linear interpolation.

You could use interpolate, which by default performs a linear interpolation, setting limit_direction to both so it fills both forward and backward:

df['A'] = df.A.interpolate(limit_direction='both')

     A
0  15.00
1  15.00
2  15.00
3  14.00
4  13.00
5  12.00
6  10.25
7   8.50
8   6.75
9   5.00


来源:https://stackoverflow.com/questions/57109931/pandas-replace-zero-as-the-nearest-average-non-zero-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!