Home > Blockchain >  Using regex and its flags with .eq() function in python to ignore case
Using regex and its flags with .eq() function in python to ignore case

Time:02-03

I've checked the docs (https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.eq.html)

I'm thinking something like below where I can use and re.I to ingnore case or use any other flag for that matter.

df.column.eq('Male').sum()

CodePudding user response:

You can use the Series.str.contains function with case=False argument, ^Male$ as regex pattern and the regex=True argument:

df['column'].str.contains('^Male$', case=False, regex=True).sum()

See the Series.str.contains documentation.

Also, see What do ^ and $ mean in a regular expression?

  •  Tags:  
  • Related