Change axis for pandas replace ffill

2019-07-21 15:32发布

问题:

Suppose I have a dataframe that looks like:

df =
       0    1    2
  0  1.0  2.0  3.0
  1  4.0  5.0  NaN
  2  6.0  NaN  NaN

Then it is possible to use df.fillna(method='ffill', axis=1) to obtain:

     0    1    2
0  1.0  2.0  3.0
1  4.0  5.0  5.0
2  6.0  6.0  6.0

i.e. I forward fill the rows. However, now I have a dataframe with -1 instead of np.nan. Pandas has the replace function that also has the possibility to use method='ffill', but replace() does not take an axis argument, so to obtain the same result as above, I would need to call df.T.replace(-1, method='ffill').T. Since transposing is quite expensive (especially considering I'm working on a dataframe of multiple gigabytes), this is not an option. How could I achieve the desired result?

回答1:

Use mask and ffill

df.mask(df.eq(-1)).ffill(axis=1)

     0    1    2
0  1.0  2.0  3.0
1  4.0  5.0  5.0
2  6.0  6.0  6.0


回答2:

IIUC, use mask and ffill with axis=1:

Where df1 = df.fillna(-1.0)

df1.mask(df1 == -1).ffill(1)

Output:

     0    1    2
0  1.0  2.0  3.0
1  4.0  5.0  5.0
2  6.0  6.0  6.0


回答3:

You can convert your -1 values to NaN before using pd.DataFrame.ffill:

print(df)

     0    1    2
0  1.0  2.0  3.0
1  4.0  5.0 -1.0
2  6.0 -1.0 -1.0

res = df.replace(-1, np.nan)\
        .ffill(axis=1)

print(res)

     0    1    2
0  1.0  2.0  3.0
1  4.0  5.0  5.0
2  6.0  6.0  6.0