Skip to content
Advertisement

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

Advertisement

Answer

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'])
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement