Blackchat83
Blackchat83

Reputation: 85

filtering PANDAS dataframe : error: missing ), unterminated subpattern at position 2988

I am learning Pandas but still at a quite basic level.

I am runnning the following line to filer a dataframe based on a list (WOODProds):

df['Only wood1']=df['Products'].str.contains('|'.join(WOODprods),na=False)

I get this error which I read should be related to some escaping character, but I cannot figure this out.

error: missing ), unterminated subpattern at position 2988

If I filter with other lists (so not with WOODProds) it works, so I assume the problem is with this specific list - which I imported from an excel column with .tolist().

Any suggestions? Thank you!

Upvotes: 1

Views: 505

Answers (1)

jezrael
jezrael

Reputation: 863291

There is problem some regex special value(s) in WOODprods list, so is necessary escpae values by re.escape:

import re
df['Only wood1']=df['Products'].str.contains('|'.join(re.escape(x) for x in WOODprods),na=False)

Upvotes: 2

Related Questions