Home > OS >  Replacing a string that appears as '??'
Replacing a string that appears as '??'

Time:09-16

I am attempting to replace '??' in the following string: 'Martin ??degaard' the ?? should be an Ø but I am trying to replace it with just an O.

The string is located in a dataframe and I have attempted this using the following code:

df = df.replace('??', 'O', regex=True)

I end up getting the following error:

re.error: nothing to repeat at position 0

Any help would be much appreciated

CodePudding user response:

? is a special character in RegEx and needs to be escaped. The RegEx to match ?? literally is \?\?.

The issue is reproducible without pandas just using the re module, so ideally your MRE should not have used pandas. Here is the working code just with re, which will work by extension for your dataframe:

import re
str_ = 'Martin ??degaard'
print(re.sub('\?\?', 'O', str_))

Output:

Martin Odegaard

So use:

df = df.replace('\?\?', 'O', regex=True)

CodePudding user response:

ljdyer is correct.

https://www.rexegg.com/regex-quickstart.html#morequants is a resource which details what different characters represent and how they are used within regex.

  • Related