Quantcast
Channel: Fill nan value when condition meet - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Fill nan value when condition meet

$
0
0

I have a dataframe as shown below:

df = pd.DataFrame({'A': [1, np.nan, np.nan, np.nan, 3, 3, 3, 3, np.nan, np.nan, np.nan, 5, 5, 5, 6, 6, 6, np.nan, np.nan, np.nan, 6, 7, 8, 9, 10,np.nan, np.nan, 10, 11]})

I wanna fill the nan value only when the previous non-nan value equals the flowing non-nan values.

Expected Output:

df = pd.DataFrame({'A': [1, np.nan, np.nan, np.nan, 3, 3, 3, 3, np.nan, np.nan, np.nan, 5, 5, 5, 6, 6, 6, np.nan, np.nan, np.nan, 6, 7, 8, 9, 10, np.nan, np.nan,10, 11],'fill_nan': [1, np.nan, np.nan, np.nan, 3, 3, 3, 3, np.nan, np.nan, np.nan, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 7, 8, 9, 10, 10, 10,10, 11]})


Viewing all articles
Browse latest Browse all 2

Trending Articles