I have imported data from excel file into a variable df using pandas. Some of the values are zeros. I need to replace this values with the average of the values in the upper row and lower row of the same column. Please suggest how I can iterate and calculate the average values.
use intrpolate
after replacing 0s with NaNs
import numpy as np
df = pd.DataFrame({ 'A' : [2,3,0,1]})
df.replace(0,np.nan, inplace = True)
df.interpolate(inplace=True)
df
this returns
A
0 2.0
1 3.0
2 2.0
3 1.0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.