EGM8686
EGM8686

Reputation: 1572

Pandas pandas backward fill from another column

I have a df like this:

val1 val2
9    3
2    .
9    4
1    .
5    1

How can I use bfill con val2 but referencing val1, such that the dataframe results in:

val1 val2
9    3
2    9
9    4
1    3
5    1

So the missing values con val2 are the previous value BUT from val1

Upvotes: 0

Views: 304

Answers (2)

Nk03
Nk03

Reputation: 14949

I guess you want ffill not bfill:

STEPS:

  1. Use mask to make values in val1 column NaN.
  2. ffill the val1 column and save the result in the variable m.
  3. fill the NaN values in val2 with m.
m = df.val1.mask(df.val2.isna()).fillna(method ='ffill')
df.val2 = df.val2.fillna(m)
OUTPUT:
   val1  val2
0     9     3
1     2     9
2     9     4
3     1     9
4     5     1

Upvotes: 1

Andrew Eckart
Andrew Eckart

Reputation: 1728

You can fill NA values in the second column with the first column shifted down one row:

>>> import pandas as pd
>>> df = pd.DataFrame({"val1": [9, 2, 9, 1, 5], "val2": [3, None, 4, None, 1]})
>>> df
   val1  val2
0     9   3.0
1     2   NaN
2     9   4.0
3     1   NaN
4     5   1.0
>>> df["val2"].fillna(df["val1"].shift(), inplace=True)
>>> df
   val1  val2
0     9   3.0
1     2   9.0
2     9   4.0
3     1   9.0
4     5   1.0

Upvotes: 1

Related Questions