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'
Answers:
There is a typo here:
df = df.loc[df['Status'] != 'Completed', 'Date'] = ''
Should be
df.loc[df['Status'] != 'Completed', 'Date'] = ''
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'
There is a typo here:
df = df.loc[df['Status'] != 'Completed', 'Date'] = ''
Should be
df.loc[df['Status'] != 'Completed', 'Date'] = ''