Search code examples
pandasdataframereplacecell

Pandas: replacing part of a string from elements in different columns


I have a dataframe where numbers contained in some cells (in several columns) look like this: '$$10'

I want to replace/remove the '$$'. So far I tried this, but I does not work:

replace_char={'$$':''}

df.replace(replace_char, inplace=True) 

Solution

  • An example close to the approach you are taking would be:

    df[col_name].str.replace('\$\$', '')
    

    Notice that this has to be done on a series so you have to select the column you would like to apply the replace to.

        amt
    0  $$12
    1  $$34
    
    df['amt'] = df['amt'].str.replace('\$\$', '')
    df
    

    gives:

      amt
    0  12
    1  34
    

    or you could apply to the full df with:

    df.replace({'\$\$':''}, regex=True)