0
Games                          Home                   Away
Team 1 vs. Team 2              Team 1                 Team 2
Team 1 @ Team 2                Team 2                 Team 1

I have a column called Games and want to split it into two new columns label as Home and Away. For the @ I used df['Away'] = df['Games'].map(lambda x: x.split('@')[0]) and it works. But I tried using df['Away'] = df['Games'].map(lambda x: x.split('vs.')[1]) it didn't work.

What am I missing??

EdChum
  • 376,765
  • 198
  • 813
  • 562

1 Answers1

0

It's not clear from the information you provided exactly what is going wrong here. But pandas provides tools that are specific to this kind of work and likely to provide informative errors if things go wrong.

Take a look at the string methods documentation. Something like df['Games'].str.extract('(.*)(vs.|@)(.*)') might be best here. Or the str.split method, but I like extract better.

Dan Allan
  • 34,073
  • 6
  • 70
  • 63