Home > Blockchain >  Replacing string with special characters in Pandas column
Replacing string with special characters in Pandas column

Time:07-03

I have a large pandas dataframe where one of the columns has weird formatting. I am tring to replace the string, but I keep getting error messages saying: 'error: unterminated character set at position 0'. An example small dataframe is below:

col 1    col 2
Amy      [{'?username': 'usr/AMY16548'}]
Jack     [{'?username': 'usr/JACK15822'}]
Sarah    [{'?username': 'usr/SARAH00001'}]

Desired result

col 1    col 2
Amy      usr/AMY16548
Jack     usr/JACK15822
Sarah    usr/SARAH00001

CodePudding user response:

Little bit barbaric way, I m sure there is pandas way how to do this, but i dont know it.

y=0
for x in df.iloc[:,1]:
    #print (x)
    text=str(x)
    index = text.find('usr')
    df.iloc[y,1]=text[index:-2]
    y =1
print (df)

CodePudding user response:

That is because whatever regex pattern that you are using has an opening [ with no associated closing ]. Unless you really are trying to use a regex character set (see https://blog.finxter.com/python-character-set-regex-tutorial/), you probably just want to escape the first [ in your pattern - like \[. Seeing your code would help.

  • Related