Pandas – Blanking out values of one column if value of another column has a specific value

Question:

I have the following DataFrame:

import pandas as pd

df = pd.DataFrame({'Status': ['','To Do', '','Completed', 'To Do', 'In Progress', 'Completed'], 
'Date': ['','9/1/2022','','12/5/2019','8/12/2020','4/19/2020','12/31/2018']})

I want to blank out the Date column for those rows where the Status is anything but "Completed". Using .loc, I am getting an error.

df = df.loc[df['Status'] != 'Completed', 'Date'] = ''

AttributeError: 'str' object has no attribute 'loc'
Asked By: Michael Kessler

||

Answers:

There is a typo here:

df = df.loc[df['Status'] != 'Completed', 'Date'] = ''

Should be

df.loc[df['Status'] != 'Completed', 'Date'] = ''
Answered By: Juliano Negri
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.