Replace missing and inconstant value, Pythons

Viewed 223

having the following example:

 import pandas as pd
df = pd.DataFrame({ 'Column A' : ['null',20,30,40,'null'],'Column B' : [100,'null',30,50,'null']});

The link for the example

I need a Python function that takes two columns and compare them:

  1. In case one column is a missing value, we fill it from the other column.

  2. In case both values are 'NULL', we Keep 'NULL'.

  3. In case of different values (inconsistent) replace both values with 'NULL'

  4. return with one attribute

the data should look like this after running the function. the link for the result

this is what I did so far, I need help implementing step number 3

def myFunction(firAttribute,secAttribute):
    x=df.ix[:,[firAttribute,secAttribute]]
    x['new']=x[firAttribute].fillna(x[secAttribute])
    x['new2']=x[secAttribute].fillna(x[firAttribute])
    x['new'] =x['new'].fillna(x['new2'])
    return x['new'] 
1 Answers
Related