Search code examples
pythonpandasdataframelogical-or

Or logic not working when there are NaN values using Python


I have dataframe (df) like below

address1            address2            Subject
NaN                 jesicca@gmail.com   Invoice 3
NaN                 NaN                 Invoice 4
rahul@gmail.com     shuan@gmail.com     Invoice 5

My logic is as below

  1. If value is present for address1 and address2 is present then to_address value should be Address1

  2. If no values are present are in Address 1 and Address 2 or both are NaN then "david@gmailcom" should be taken

  3. If address1 value is not prsent or NaN and address2 value is present then this should be taken.

But my or-logic code doesnt work as required. What is the mistake I am making.

My code:

for i, row in df.iterrows():
    subject  = row["Subject"]
    to_address = row['address1'] or row['address2'] or "david@gmailcom"

Solution

  • Try to do it with a lambda function:

    df = pd.DataFrame([[float("nan"),"jesicca@gmail.com","Invoice 3"],[float("nan"),float("nan"),"Invoice 4"],
                       ["rahul@gmail.com","shuan@gmail.com","Invoice 5"]], columns = ["address1","address2","Subject"])
    df["case"] = df.apply(lambda x: x["address1"] if not pd.isna(x["address1"]) \
                                    else x['address2'] if not pd.isna(x["address2"]) \
                                    else "david@gmailcom", axis = 1)
    df
    
        address1        address2            Subject     case
    0   NaN             jesicca@gmail.com   Invoice 3   jesicca@gmail.com
    1   NaN             NaN                 Invoice 4   david@gmailcom
    2   rahul@gmail.com shuan@gmail.com     Invoice 5   rahul@gmail.com