0

I have a list

l = [1,2,3,4]

and would like to use this to dynamically create empty dataframes to later append data to:

for n in l:
    df+str(n) = pd.DataFrame(columns=['col','val'])

so you can see the output would be 4 empty dataframe, df1, df2, df3, and df4. But when executed I get a SyntaxError: keyword can't be an expression. How do I go about this?

user3486773
  • 1,174
  • 3
  • 25
  • 50
  • 1
    It's a bad idea to create variable identifiers based on the value of other variables. Try using a dictionary. – crissal Aug 13 '21 at 05:44
  • The proper data structure in this case is a dictionary. Dynamically creating variables is possible using `globals()` but it's an anti-pattern in most programming languages. – NotAName Aug 13 '21 at 05:45

0 Answers0