how to select rows which one of its columns values contains specific string in python?
I have used the one mentioned here and got errors while I used sample data frame and it looks fine, I am suspicious about my own dataframe which I am reading from a file but still can't guess what the issue is :
df=pd.read_csv("location",encoding = "ISO-8859-1") # readCSV
df[df['DESCRIPTION'].str.contains('+')
errors:
File "C:\ProgramData\Anaconda3\lib\sre_parse.py", line 616, in _parse
source.tell() - here + len(this))
error: nothing to repeat
解决方案
+ is special regex char (match one or more repetitions), so need escape it:
df = pd.DataFrame({'DESCRIPTION': ['aa+','a','+']})
df = df[df['DESCRIPTION'].str.contains('\+')]
print(df)
DESCRIPTION
0 aa+
2 +
Or add parameter regex=False:
df[df['DESCRIPTION'].str.contains('+', regex=False)]