How to pass multiple flags to string extract method in pandas?

Viewed 337

I want to pass two flags in the pd.series.str.extract() method. How can I do that?

I know how to do with one flag -

# Regrex pattern
team_regex_new = r"""
(Rajasthan\sRoyals|Kings\sXI\sPunjab|Chennai\sSuper\sKings|Delhi\sCapitals|Mumbai\sIndians|Kolkata\sKnight\sRiders|
Royal\sChallengers\sBangalore|Deccan\sChargers|Kochi\sTuskers\sKerala|Pune\sWarriors|Sunrisers\sHyderabad|
Gujarat\sLions|Rising\sPune\sSupergiant|No\sresult|Match\sabandoned)
"""

# applied to the pandas series
df['Result'].str.extract(team_regex_new, flags=re.VERBOSE)

output - 
    0
1   Chennai Super Kings
2   Kolkata Knight Riders
3   Delhi Capitals
4   Kings XI Punjab
5   Chennai Super Kings
6   Kolkata Knight Riders

Apart from the re.VERBOSE flag, i also want to add re.IGNORECASE flag to the extract method.

# trying to do something like this
df['Result'].str.extract(team_regex_new, flags=[re.VERBOSE,re.IGNORECASE])

I tried to pass it as a list, tuple , string. Nothing is working. How can I pass both flags together?

1 Answers

You can use | - found it here:

Multiple flags can be combined with the bitwise OR operator, for example re.IGNORECASE | re.MULTILINE.

df['Result'].str.extract(team_regex_new, flags= re.VERBOSE | re.IGNORECASE)
Related