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?
>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.