Forward fill dataframe column if string is present in another column

Question:

I have a dataframe and I want to forward fill one of the columns but only if the string is present in one of the other columns.

       Type                                                 Match     Matchup
0    Parent  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_NYY-vs-LAA  NYY-vs-LAA
1     Child  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_NYY-vs-LAA         NaN
2  SubChild  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_NYM-vs-LAD  NYM-vs-LAD
3  SubChild  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_NYM-vs-LAD         NaN
4      Test                                All_Star_Game_12252000         NaN
5     Child  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_BAL-vs-BOS  BAL-vs-BOS
6    Parent  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_BAL-vs-BOS         NaN
7     Child  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_TBR-vs-COL  TBR-vs-COL
8    Parent  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_TBR-vs-COL         NaN
9  SubChild  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_TBR-vs-COL         NaN

For example, if Matchup is present Match, I want to forward fill it so that the output looks like:

    Type                                                   Match       Matchup
  Parent    ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_NYY-vs-LAA    NYY-vs-LAA
   Child    ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_NYY-vs-LAA    NYY-vs-LAA
SubChild    ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_NYM-vs-LAD    NYM-vs-LAD
SubChild    ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_NYM-vs-LAD    NYM-vs-LAD
    Test                                  All_Star_Game_12252000    
   Child    ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_BAL-vs-BOS    BAL-vs-BOS
  Parent    ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_BAL-vs-BOS    BAL-vs-BOS
   Child    ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_TBR-vs-COL    TBR-vs-COL
  Parent    ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_TBR-vs-COL    TBR-vs-COL
SubChild    ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_TBR-vs-COL    TBR-vs-COL
Asked By: user53526356

||

Answers:

Stealing @mozway’s comment

from io import StringIO

import pandas as pd


df = pd.read_csv(
    StringIO("""
       Type                                                 Match     Matchup
0    Parent  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_NYY-vs-LAA  NYY-vs-LAA
1     Child  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_NYY-vs-LAA         NaN
2  SubChild  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_NYM-vs-LAD  NYM-vs-LAD
3  SubChild  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_NYM-vs-LAD         NaN
4      Test                                All_Star_Game_12252000         NaN
5     Child  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_BAL-vs-BOS  BAL-vs-BOS
6    Parent  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_BAL-vs-BOS         NaN
7     Child  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_TBR-vs-COL  TBR-vs-COL
8    Parent  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_TBR-vs-COL         NaN
9  SubChild  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_TBR-vs-COL         NaN
"""),
    sep="s+"
)

df.update(df.groupby("Match")["Matchup"].ffill())

print(df)
       Type                                                 Match     Matchup
0    Parent  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_NYY-vs-LAA  NYY-vs-LAA
1     Child  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_NYY-vs-LAA  NYY-vs-LAA
2  SubChild  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_NYM-vs-LAD  NYM-vs-LAD
3  SubChild  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_NYM-vs-LAD  NYM-vs-LAD
4      Test                                All_Star_Game_12252000         NaN
5     Child  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_BAL-vs-BOS  BAL-vs-BOS
6    Parent  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_BAL-vs-BOS  BAL-vs-BOS
7     Child  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_TBR-vs-COL  TBR-vs-COL
8    Parent  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_TBR-vs-COL  TBR-vs-COL
9  SubChild  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_TBR-vs-COL  TBR-vs-COL

References

Answered By: Ian Thompson
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.