0

I have created a dataframe on my local machine like so:

df1 = pd.DataFrame()

Next, I have added to columns to dataframe plus I want to assign a string to only one column. I am attempting to do this like so:

df1['DF_Name'] = 'test'
df1['DF_Date'] = ''

The columns get added successfully, but the string 'test' does not. When I apply this to other dataframe it works perfectly fine. What am I doing wrong?

I am also trying to append dates into the same dataframe except using logic, and that is also not working. Logic is as so:

if max_created > max_updated:
    df1['DF_Date'] = max_created
else:
    df1['DF_Date'] = max_updated

Not sure what I am doing wrong.

Thank you in advance.

1 Answer 1

1

You need to add brackets, like:

df1 = pd.DataFrame()
df1['DF_Name'] = ['test']
df1['DF_Date'] = ['']
df1

you can't assign a single value to a pd.Series. With the brackets, it's a list instead.

Sign up to request clarification or add additional context in comments.

2 Comments

why do you need the brackets?
with the brackets, it's a list with a string in it, not a string anymore. Try: type(['Hello']) and type('Hello') to see the difference.

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.