Search code examples
pythonpandasdataframemergedrop

How to replace all "False" values in any columns by values from other columns with the same names if that values are not False


for example:

import pandas as pd


dc1 = {
    'n':[1, 2, 3, 4],
    'a':[0, 2, 0.0, 4],
    'b':[0,'', False, 5],
    }

dc2 = {
    'n':[1, 2, 3, 4],
    'a':[1, 0, 3, 0.0],
    'b':[6, 5, 8, 9],
    }

Merge on 'n' column. Is it possible values in 'a'(and 'b') column in df1 is replaced by values of 'a' (and 'b') column in df2 if value of 'a'(and 'b') in df1 is False (or some values that we can manually set [False, 0, 0.0, 'Nan', None, '','Null']), and then drop column with suffix _drop?

In result (dict for example) must be that:

dc_result = {
    'n':[1, 2, 3, 4],
    'a':[1, 2, 3, 4],
    'b':[6, 5, 8, 5],
    }

Solution

  • You can use isin to get the indexes where a cell in a or b contains an element from a manual list: [False, 0, 0.0, 'Nan', None, '','Null'] and use np.where to replace those elements:

    replace_list = [False, 0, 0.0, 'Nan', None, '','Null']
    df1[['a', 'b']] = ( np.where(df1[['a', 'b']].isin(replace_list), df2[['a', 'b']], 
                                                       df1[['a', 'b']]).astype(int) )