Link to home
Start Free TrialLog in
Avatar of Massimo Scola
Massimo ScolaFlag for Switzerland

asked on

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"
        break
    else:
        customers_df['complete_address'] = np.nan

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of aikimark
aikimark
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Do you need any more help with this?