Search code examples
pythonpandasnumpyfillna

How to convert if/else to np.where in pandas


My code is below

apply pd.to_numeric to the columns where supposed to int or float but coming as object. Can we convert more into pandas way like applying np.where

if df.dtypes.all() == 'object':
    df=df.apply(pd.to_numeric,errors='coerce').fillna(df)
else:
    df = df

Solution

  • A simple one liner is assign with selest_dtypes which will reassign existing columns

    df.assign(**df.select_dtypes('O').apply(pd.to_numeric,errors='coerce').fillna(df))
    

    np.where:

    df[:] = (np.where(df.dtypes=='object',
              df.apply(pd.to_numeric,errors='coerce').fillna(df),df)
    

    Example (check Price column) :

    d = {'CusID': {0: 1, 1: 2, 2: 3},
         'Name': {0: 'Paul', 1: 'Mark', 2: 'Bill'},
         'Shop': {0: 'Pascal', 1: 'Casio', 2: 'Nike'},
         'Price': {0: '24000', 1: 'a', 2: '900'}}
    df = pd.DataFrame(d)
    
    print(df)
       CusID  Name    Shop  Price
    0      1  Paul  Pascal  24000
    1      2  Mark   Casio      a
    2      3  Bill    Nike    900
    
    df.to_dict()
    {'CusID': {0: 1, 1: 2, 2: 3},
     'Name': {0: 'Paul', 1: 'Mark', 2: 'Bill'},
     'Shop': {0: 'Pascal', 1: 'Casio', 2: 'Nike'},
     'Price': {0: '24000', 1: 'a', 2: '900'}}
    

    (df.assign(**df.select_dtypes('O').apply(pd.to_numeric,errors='coerce')
       .fillna(df)).to_dict())
    
    {'CusID': {0: 1, 1: 2, 2: 3},
     'Name': {0: 'Paul', 1: 'Mark', 2: 'Bill'},
     'Shop': {0: 'Pascal', 1: 'Casio', 2: 'Nike'},
     'Price': {0: 24000.0, 1: 'a', 2: 900.0}}