The Great
The Great

Reputation: 7703

Copy non-na rows to fill non-na columns using pandas

I have a dataframe like as shown below

df = pd.DataFrame({'person_id': [101,101,101,101],
                   'sourcename':['test1','test2','test3','test4'],
                            'Test':[np.nan,np.nan,'B5','B6']})

What I would like to do is copy non-na rows from Test column and paste it in corresponding row under sourcename column

When I tried the below, it makes the other rows of sourcename column as NA

df['sourcename'] = df.loc[df['Test'].notna()]['Test']

I expect my output to be like as shown below

enter image description here

Upvotes: 2

Views: 670

Answers (2)

Shubham Sharma
Shubham Sharma

Reputation: 71689

Series.update

We can update the values in sourcename column with the non NaN values from Test column

df['sourcename'].update(df['Test'])

>>> df

   person_id sourcename Test
0        101      test1  NaN
1        101      test2  NaN
2        101         B5   B5
3        101         B6   B6

Upvotes: 5

jezrael
jezrael

Reputation: 862611

One idea with Series.fillna:

df['sourcename'] = df['Test'].fillna(df['sourcename'])

Solution with check non missing values:

You are close, assign to rows filtered by mask:

df.loc[df['Test'].notna(), 'sourcename'] = df['Test']

Or:

df['sourcename'] = np.where(df['Test'].notna(), df['Test'], df['sourcename'])

Upvotes: 1

Related Questions