Search code examples
pythonpandasregextext-extraction

Python Pandas Extract text between a word and a symbol


I am trying to extract text between a word and a symbol.

Here is the input table.

enter image description here

And my expected output is like this.

enter image description here

I do not want to have the word 'Team:' and '<>' in the output.

I tried something like this but it keeps the 'Team:' and '<>' in the output: data[new col]=data['Team'].str.extract(r'(Team:\s[a-zA-Z\s]+<>)

Thank you.


Solution

  • Use regex captured group for str.extract method:

    df['Team'].str.extract(r'^Team: ([^<>]+)')
    

    • [^<>]+ - matches any character except < and > chars