Home > front end >  Fill NA only between two values in the same column
Fill NA only between two values in the same column

Time:05-04

I have a dataframe like this:

                   VESSEL_NAME FAILURE
0          AAGI - AKAGISAN     LCA
1          AAGI - AKAGISAN     NaN
2          AAGI - AKAGISAN     NaN
3          AAGI - AKAGISAN     NaN
4          AAGI - AKAGISAN     NaN
5          AAGI - AKAGISAN     NaN
6          AAGI - AKAGISAN     TCA
7   AAGI - SHANDONG DA REN     NaN
8   AAGI - SHANDONG DA REN     LCA
9   AAGI - SHANDONG DA REN     NaN
10  AAGI - SHANDONG DA REN     NaN
11  AAGI - SHANDONG DA REN     NaN
12  AAGI - SHANDONG DA REN     NaN
13  AAGI - SHANDONG DA REN     TCA
14     AAGI - SEA MARANHAO     LCA
15     AAGI - SEA MARANHAO     NaN
16     AAGI - SEA MARANHAO     NaN
17     AAGI - SEA MARANHAO     NaN
18     AAGI - SEA MARANHAO     TCA

I need to "ffill" the column "FAILURE" only if the previous row value is "LCA" like the expected DF:

               VESSEL_NAME FAILURE
0          AAGI - AKAGISAN     LCA
1          AAGI - AKAGISAN    FILL
2          AAGI - AKAGISAN    FILL
3          AAGI - AKAGISAN    FILL
4          AAGI - AKAGISAN    FILL
5          AAGI - AKAGISAN    FILL
6          AAGI - AKAGISAN     TCA
7   AAGI - SHANDONG DA REN     NaN
8   AAGI - SHANDONG DA REN     LCA
9   AAGI - SHANDONG DA REN    FILL
10  AAGI - SHANDONG DA REN    FILL
11  AAGI - SHANDONG DA REN    FILL
12  AAGI - SHANDONG DA REN    FILL
13  AAGI - SHANDONG DA REN     TCA
14     AAGI - SEA MARANHAO     LCA
15     AAGI - SEA MARANHAO    FILL
16     AAGI - SEA MARANHAO    FILL
17     AAGI - SEA MARANHAO    FILL
18     AAGI - SEA MARANHAO     TCA

Is there any way to do it? I have tried a few attempts but none have been successful.

CodePudding user response:

What about:

import pandas as pd

df = pd.DataFrame({'FAILURE':['TCA', np.nan, 'LCA', np.nan, np.nan, 'TCA', np.nan, 'LCA', np.nan]})

fill_value = 'FILL'
where_value = 'LCA'

df['FAILURE'] = df['FAILURE'].fillna(fill_value).where(df['FAILURE'].fillna(method='ffill').eq(where_value) | df['FAILURE'].notna())

Input:

  FAILURE
0     TCA
1     NaN
2     LCA
3     NaN
4     NaN
5     TCA
6     NaN
7     LCA
8     NaN

Result:

  FAILURE
0     TCA
1     NaN
2     LCA
3    FILL
4    FILL
5     TCA
6     NaN
7     LCA
8    FILL
  • Related