1

I have a data table:

Index Value

0      NaN

1      1.15

2      2.25

3      2.33

Condition: First check wherever previous row value is not NaN then replace current row value with previous row value.

Desired output:

Index Value

0      NaN

1      1.15

2      1.15

3      1.15
1
  • 1
    Pure code-writing requests are off-topic on Stack Overflow — we expect questions here to relate to specific programming problems — but we will happily help you write it yourself! Tell us what you've tried, and where you are stuck. This will also help us answer your question better. Commented May 13, 2020 at 7:34

1 Answer 1

1

Compare values for missing values, then get first consecutive value and replace another by DataFrame.where, forward filling missing values and last replace original missing values:

df = pd.DataFrame({'Value':[np.nan,1.15,2.15,3.15,np.nan,2.1,2.2,2.3]})

m = df.notna()
df1 = df.where(m.ne(m.shift())).ffill().where(m)

print (df1)
   Value
0    NaN
1   1.15
2   1.15
3   1.15
4    NaN
5   2.10
6   2.10
7   2.10

Details:

print (m.ne(m.shift()))
  Value
0   True
1   True
2  False
3  False
4   True
5   True
6  False
7  False

print (df.where(m.ne(m.shift())))
 Value
0    NaN
1   1.15
2    NaN
3    NaN
4    NaN
5   2.10
6    NaN
7    NaN

print (df.where(m.ne(m.shift())).ffill())
   Value
0    NaN
1   1.15
2   1.15
3   1.15
4   1.15
5   2.10
6   2.10
7   2.10
Sign up to request clarification or add additional context in comments.

4 Comments

When the df is pd.DataFrame({'Value':[np.nan,1.15,2.15,3.15,np.nan,2.1,2.2,2.3]}) the expected output is NaN 1.15 1.15 1.15 NaN 2.1 2.1 2.1 but this gives NaN 1.15 1.15 1.15... all 1.15
@jezrael Thanks. Also it would be nice if you can explain it.
@jezrael Coolest answer. +1 ;)
@jezrael One more question I have a dataframe pd.Dataframe({"Value:[np.nan,1,5,np.nan,6,-1,5,np.nan,6]}), here to logic should reset after every -1 so desired output expected is NaN 1 .5 1. 5 6 -1 5 5 5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.