Syed Fahad Sultan
Syed Fahad Sultan

Reputation: 508

For certain rows, copying values of a set of columns to another, of the same data frame

I was expecting the following line of code to copy values from col4 and col5 (for rows that satisfy the filter) to col1 and col2 of the same rows. However, col1 and col2 remain unchanged.

df[['col1', 'col2']][df['col3'].isnull()] = df[df['col3'].isnull()][['col4', 'col5']]

Upvotes: 0

Views: 37

Answers (1)

maxymoo
maxymoo

Reputation: 36545

Only use the mask on the RHS:

df[['col1', 'col2']] = df[df['col3'].isnull()][['col4', 'col5']]

Upvotes: 1

Related Questions