1

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
Ali AzG
  • 1,861
  • 2
  • 18
  • 28
f.a
  • 101
  • 1
  • 5
  • 14

1 Answers1

0

+ 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)]
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252