0

Given the following dataframe:

A    B   C
NULL 1   2
3    4   3
5    3   1
NULL 2   6

I'd like to replace the value in B with null if the A is null:

A     B    C
NULL  NULL 2
3     4    3
5     3    1
NULL  NULL 6

Any ideas?

0

1 Answer 1

1

You can use pd.DataFrame.loc for this.

Note that any column with NaN values becomes float, as NaN is considered a float.

df.loc[df['A'].isnull(), 'B'] = np.nan

print(df)

#      A    B  C
# 0  NaN  NaN  2
# 1  3.0  4.0  3
# 2  5.0  3.0  1
# 3  NaN  NaN  6
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.