- A+

Category：Languages

The goal is to maintain the relationship between two columns by setting to NaN all the values from one column in another column.

Having the following data frame:

`df = pd.DataFrame({'a': [np.nan, 2, np.nan, 4],'b': [11, 12 , 13, 14]}) a b 0 NaN 11 1 2 12 2 NaN 13 3 4 14 `

Maintaining the relationship from column `a`

to column `b`

, where all NaN values are updated results in:

` a b 0 NaN NaN 1 2 12 2 NaN NaN 3 4 14 `

One way that it is possible to achieve the desired behaviour is:

`df.b.where(~df.a.isnull(), np.nan) `

Is there any other way to maintain such a relationship?

You could use `mask`

on `NaN`

rows.

`In [366]: df.mask(df.a.isnull()) Out[366]: a b 0 NaN NaN 1 2.0 12.0 2 NaN NaN 3 4.0 14.0 `

For, presence of any `NaN`

across columns use `df.mask(df.isnull().any(1))`