简体   繁体   中英

Python Pandas: Replace NaN with neighboring value

I need to analyze a Dataframe, which bases on recorded data of an experiment. Each row of my dataframe resembles a measurement with given recording frequency.

Since every measurement took a different amount of time, there were lots of NaN values. I would like to replace now those cells to bring my dataframe to an equal number of entrys.

Due to the characteristics of my measurement the value NaN would mean a measurement of the value in the column left of it. I was recording the position of an object. When the object stopped moving, the measurement was stopped as well.

For that purpose I iterated over each column and when the value is NaN i wanted to replace it with the value left of it. k is the number of columns, u the number of rows. i and v are running numbers of row and column:

while v < u
    i = 0
    while i < k:
          if df.loc[v][i] == NaN:
             df.iat[v][i] = df.loc[v][i-1]
          i = i + 1
    v = v + 1

I tried it with multiple commands (eg pd.isna.df.iloc[v,i]) but none of it works. Could you help me out? Thanks a lot!

Try df[col] = df[col].fillna(df[left_col])

Try with fillna(axis=1) :

new = df.fillna(method='ffill',axis=1)

This would fill all your columns when blank, with their respective value on the left.

Better should be ffill only:

df = df.ffill(axis=1)

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.

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