Split columns with pandas

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 I want to split it into two new columns representing Home and Guests. For @ I used df['Away'] = df['Games'].map(lambda x: x.split('@')[0])

and it works. But I tried to use it df['Away'] = df['Games'].map(lambda x: x.split('vs.')[1])

, it didn't work.

What am I missing?

0


source to share


1 answer


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



Take a look at the documentation for string methods . Something like this might be better here df['Games'].str.extract('(.*)(vs.|@)(.*)')

. Or a method str.split

, but I like to extract better.

0


source







All Articles