Search code examples
pythonpandaseval

Checking if values in multiple fields are null relative to each other?


So I am using the eval function to perform operations on various fields. I wish to continue using eval. Consider the expression result = fieldA + feildB My problem is however that if there is a null values in one of the fields, eval returns null instead of treating it as a 0 in the case of an addition formula. My current outputted table looks as follows:

fieldA fieldB result
null null null
null 5 null
4 5 9
0 6 6

I want to evaluate if one of the values(not both) is null to then fillna(0). If both are null I would like both fields to stay null. My desired outputted table should look like this:

fieldA fieldB result
null null null
0 5 5
4 5 9
0 6 6

I am pretty lost as to where to begin with such a conditional so any help would be really appreciated. I could always butcher this with loops but I am always very relucent to go down that path with pandas.


Solution

  • Without eval you would need to use add and the fill_value=0 parameter:

    df['result'] = df['fieldA'].add(df['fieldB'], fill_value=0)
    

    You can use the same syntax with eval:

    out = df.eval('result = fieldA.add(fieldB, fill_value=0)')
    

    Alternatively:

    out = df.eval('result = fieldA.fillna(0) + fieldB.fillna(0)')
    

    Output:

       fieldA  fieldB  result
    0     NaN     NaN     NaN
    1     NaN     5.0     5.0
    2     4.0     5.0     9.0
    3     0.0     6.0     6.0
    

    Reproducible input:

    from numpy import nan
    
    df = pd.DataFrame({'fieldA': [nan, nan, 4.0, 0.0],
                       'fieldB': [nan, 5.0, 5.0, 6.0]})