Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
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?
You can use pd.DataFrame.loc for this.
pd.DataFrame.loc
Note that any column with NaN values becomes float, as NaN is considered a float.
NaN
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
Add a comment
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.