Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

python pandas problem for regular expression

The code df.loc[df["University"].str.contains("^s[a-z]"),regex=True] gives me the error below:

File "/tmp/ipykernel_27/251161980.py", line 7
    df.loc[df["University"].str.contains("^s[a-z]"), regex=True]
                                                         ^
SyntaxError: invalid syntax

How can I fix it?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You have to put the regex=True in the .contains() parenthesis. The correct code may look like this:

df.loc[df["University"].str.contains("^s[a-z]", regex=True)]

I strongly suggest understanding the code instead of just trying different things. For example, the regex=True is a method parameter. It should be between the parentheses to be used in the .contains() method. Also the [] brackets for the pandas DataFrame is trying to capture the data that contains the regex provided. So it can not contain , regex=True itself. The correct code is provided above.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading