Retrieve first non-NA value from a group of columns

Viewed 151

I'm a pretty new coder so if you guys could help me that'd be great.

Let's say I have one dataframe: enter image description here

Can I combine "Number" and "Number 2" in a way where "Number" takes precedence unless there is no value(NaN). When "Number" is NaN, then we use the entry in "Number 2"

It should look like this enter image description here

In other words, I want to combine "Number" and "Number 2" where if "Number" is NaN, then use "Number 2"'s entry. If "Number" has an entry, keep it for the merged entry. This is similar to left merge for merging two data frames.

Btw, I'm using python and pandas.

Update: trying to use np.where

enter image description here

Output without np.where: enter image description here

Output with np.where: enter image description here

2 Answers

Or you can use apply:

df['Number'] = df.apply(lambda row: row['Number'] if not pd.isnull(row['Number']) else row['Number2'], axis=1)

Just need to drop the Number2 column then

bfill(axis=1) and iloc

df = pd.DataFrame({
    'Name': df['Name'],
    'Number': df.filter(like='Number').bfill(axis=1).iloc[:,0]
})
Related