We help IT Professionals succeed at work.

Panda Dataframe - iterate through a dataframe and ignore NaN

I have a dataframe with the customer Id, first name, address1, address2 etc and I would like to merge all the address columns into one single address column.

How do I skip columns that have an empty string or None in their address1 column?

I use the following function but somehow but it looks like the loop enters the else part of the loop even though I add a break statement:

for index, row in customers_df.iterrows():
    if customers_df['address1'][index] != "None":
        customers_df['complete_address'] = customers_df["address1"].map(str) + ", " + customers_df["address2"].map(str) + ", " + customers_df["zip"].map(str) + ", " + customers_df["town"].map(str) + ", Switzerland"
        customers_df['complete_address'] = np.nan

Open in new window

Watch Question

Top Expert 2014
Do the missing/empty fields actually contain "None"?

This list comprehension would be the first thing to test.
for index, row in customers_df.iterrows():
    row['complete_address'] = ", ".join([addrpart for addrpart in [
        row["address1"].map(str), row["address2"].map(str), 
        row["zip"].map(str), row["town"].map(str), "Switzerland"] if len(addrpart) > 0

Open in new window

I'm not sure why you are casting the columns into strings.  I would think they are already strings.
Top Expert 2014

Do you need any more help with this?