0

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'
1
  • As specified by the answer bellow you don't need to assign the result of df.loc[] to df as it will modify your data frame in place. Commented Nov 6, 2022 at 23:08

1 Answer 1

3

There is a typo here:

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

Should be

df.loc[df['Status'] != 'Completed', 'Date'] = ''
Sign up to request clarification or add additional context in comments.

4 Comments

That's not a typo, I'm using df = to redefine the DataFrame based on the .loc. Regardless, this doesn't resolve the error.
First code chunk can be removed as it could be mistaken as a valid answer.
An alternative expression which does the same thing is df['Date'][df['Status'] != 'Completed'] = ''
Oh I see that now. I was used to redefining df, like when I just wanted it to reflect a certain value in a column. But this worked, many thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.