Position of words within pandas column

Try this list comprehension:

df["position"] = [ent.index("Manchester") 
                 if "Manchester" in ent else -1
                 for ent in df.Text.str.split()]

df

         Text                               position
0   Manchester United, finally, won ...      0
1   Arsenal is one of the best ...          -1
2   Beckham played for Manchester United     3
3   Manchester is a city in the UK           0

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top