RustyShackleford
RustyShackleford

Reputation: 3667

Writing string to empty dataframe not working, but works in other dataframes, how to fix?

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.

Upvotes: 0

Views: 246

Answers (1)

laurenz
laurenz

Reputation: 306

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.

Upvotes: 1

Related Questions