[SOLVED] Numpy Select with Multiple Conditions Not Returning Values

Issue

I combined three dataframes that have multiple email columns, then renamed the columns for ease of scripting. I’m trying to create a master email column based on conditions. If C is populated, use C. If not, and B is populated, use B. If not, and A is populated, use A. For some reason it’s only filling in the master column if C is populated. Even if A is populated, it’s returning a null value.

dfs = [df_A, d365, df_C]
from functools import reduce
dfFinal = reduce(lambda left,right: pd.merge(left,right,on='leadId'), dfs)

import numpy as np
emailconditions = [
dfFinal['EmailC'] is not None,
(dfFinal['EmailC'] is None) & (dfFinal['EmailB'] is not None),
(dfFinal['EmailC'] is None) & (dfFinal['EmailB'] is None)]

emailvalues = [
dfFinal['EmailC'],
dfFinal['EmailB'],
dfFinal['EmailA']]

dfFinal['emailFinal'] = np.select(emailconditions, emailvalues)

Solution

Try setting emailconditions like so:

emailconditions = [
dfFinal['EmailC'].notna(),
dfFinal['EmailC'].isna() & dfFinal['EmailB'].notna(),
dfFinal['EmailC'].isna() & dfFinal['EmailB'].isna()]

Key point is to use notna() instead of is not None and isna() instead of is None.

Answered By – richardec

Answer Checked By – Willingham (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *