Search code examples
pythonpandasstringdataframedata-cleaning

Replacing string with special characters in Pandas column


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

Solution

  • 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)