pythonpandasdataframe

How do I replace all the instances of a certain character in a dataframe?


I have a dataframe that has many instances of '?' in different rows with dtype 'object'.

How can I replace all the '?' with 0.


Solution

  • Consider the dataframe df

    df = pd.DataFrame([['?', 1], [2, '?']])
    
    print(df)
    
       0  1
    0  ?  1
    1  2  ?
    

    replace

    df.replace('?', 0)
    
       0  1
    0  0  1
    1  2  0
    

    mask or where

    df.mask(df == '?', 0)
    # df.where(df != '?', 0)
    
       0  1
    0  0  1
    1  2  0
    

    However, imagine your dataframe has ? within longer strings.

    df = pd.DataFrame([['a?', 1], [2, '?b']])
    
    print(df)
    
        0   1
    0  a?   1
    1   2  ?b
    

    replace with regex=True

    df.replace('\?', '0', regex=True)
    
        0   1
    0  a0   1
    1   2  0b