1

I have multiple dataframes with the same structure but different values for instance,

df0, df1, df2...., df9

To each dataframe I want to add a column named eventdate that consists of one date, for instance, 2019-09-15 using for loop

for i in range(0, 9);
    df+str(i)['eventdate'] = "2021-09-15"

but I get an error message SyntaxError: cannot assign to operator

I think it's because df isn't defined. This should be very simple.. Any idea how to do this? thanks.

1 Answer 1

1
dfs = [df0, df1, df2...., df9]
dfs_new = []

for i, df in enumerate(dfs):
    df['eventdate'] = "2021-09-15"
    dfs_new.append(df)

if you can't generate a list then you could use eval(f"df{str(num)}") but this method isn't recommended from what I've seen

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

2 Comments

I do not want to upend dfs.. I just want to have them separate but add a column to each df? in this case, should I just remove the last line of command?
so then you'll want to use the eval() method. you can assign the df as a tmp variable, add the column, then overwrite the original variable

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.