2

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

user3085496
  • 175
  • 1
  • 2
  • 10
  • Please provide a small set of sample data in the form of text that we can copy and paste. Include the corresponding desired result. Check out the guide on [how to make good reproducible pandas examples](https://stackoverflow.com/a/20159305/3620003). – timgeb Jun 02 '20 at 20:06

2 Answers2

2

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

Tom
  • 8,310
  • 2
  • 16
  • 36
  • 1
    This also only works for two columns and is excrusiatingly slow on larger data. See https://stackoverflow.com/questions/54432583/when-should-i-ever-want-to-use-pandas-apply-in-my-code for more. cc @user3085496 – cs95 Jun 03 '20 at 00:37
  • @cs95 I wasn't aware of this - thank you for sharing!! – Tom Jun 03 '20 at 06:16
1

bfill(axis=1) and iloc

df = pd.DataFrame({
    'Name': df['Name'],
    'Number': df.filter(like='Number').bfill(axis=1).iloc[:,0]
})
cs95
  • 379,657
  • 97
  • 704
  • 746