1

I have a dataset that looks like this.

Conatct 1 Contact 2 Contact 3
NA XYZ STU
NA NA LMN
ABC PQR NA

I'm trying to write a new column which will consolidate the contact information from the three different columns into one consolidated column such that if Contact 1 is NA then contact information from Contact 2 will be picked up and if Contact 2 is also empty then Contact 3 will get picked up. Below is the code I have so far.

def rm_contact(row):
   if row["LOB Contact #1"] != "NA":
     return row["Contact 1"]      
   else:
     return row["Contact 2"] 

df2 = df2.assign(rm=df2.apply(rm_contact, axis=1))

The else part of the code doesn't seem to work. So even though Contact 1 is NA the details from Contact 2 or Contact 3 don't get filled in properly. Any suggestions?

2 Answers 2

1

Use if NA is missing value - back filling missing values per rows and then select first column:

cols = ["Contact 1","Contact 2","Contact 3"]
df2['rm'] = df2[cols].bfill(axis=1).iloc[:, 0]

If NA is string first replace values to missing NaNs:

df2['rm'] = df2[cols].replace('NA',np.nan).bfill(axis=1).iloc[:, 0]
Sign up to request clarification or add additional context in comments.

Comments

1

Simple case of chain together fillna() in order you want values to be picked

df = pd.read_csv(io.StringIO("""Contact 1   Contact 2   Contact 3
NA  XYZ STU
NA  NA  LMN
ABC PQR NA
"""), sep="\t")

df.assign(rm_contact=df["Contact 1"].fillna(df["Contact 2"]).fillna(df["Contact 3"]))

Contact 1 Contact 2 Contact 3 rm_contact
0 nan XYZ STU XYZ
1 nan nan LMN LMN
2 ABC PQR nan ABC

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.