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 (e.g. pd.isna.df.iloc[v,i]) but none of it works. Could you help me out? Thanks a lot!
CodePudding user response:
Try df[col] = df[col].fillna(df[left_col])
CodePudding user response:
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.
CodePudding user response:
Use ffill
for forward filling misisng values NaN
or None
s per rows:
df = df.ffill(axis=1)