Skip to content
Advertisement

associate competition with each match row in pandas

I have a dataset for esports matches and I want to associate competition with each match row

    Time                team1              team2       score1   score2
0   Competition1        nan                nan         nan      nan
1   2/15/2021 4:30 AM   Reckoning          Headshot    1        0
2   Competition2        nan                nan         nan      nan
3   2/15/2021 9:00 AM   Movistar           Riders      1        0
4   2/15/2021 10:00 AM  eMonkeyz           G2 Arctic   0        1

I want it like that:

    Competition    Time                 team1              team2       score1   score2
0   Competition1   2/15/2021 4:30 AM    Reckoning          Headshot    1        0
1   Competition2   2/15/2021 9:00 AM    Movistar           Riders      1        0
2   Competition2   2/15/2021 10:00 AM   eMonkeyz           G2 Arctic   0        1

Advertisement

Answer

Let us try where with ffill after select the string with startswith

df['Competition'] = df.Time.where(df.Time.str.startswith('Competition')).ffill()
df.dropna(inplace=True)
df
Out[118]: 
               Time      team1     team2  score1  score2   Competition
1   2/15/20214:30AM  Reckoning  Headshot     1.0     0.0  Competition1
3   2/15/20219:00AM   Movistar    Riders     1.0     0.0  Competition2
4  2/15/202110:00AM   eMonkeyz  G2Arctic     0.0     1.0  Competition2
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement