-2

I have a script that fetches data from a mySQL server and saves it as a pandas dataframe, that particular dataframe then has to be saved with a unique name as chosed by the user.

the dataframe is created as part of a function called "createDF", and then saved as "df".

df = createDF()

this works and the dataframe object is saved as "df". What I need to do is allow the user to name the dataframe and not have it fixed with the name "df" but instead have it saved as "df+userinput". Where "userinput" is the string/int supplied by the user.

I seems certain that the issue is in trying to combine the input-type (str or int) with the object name "df", as I keep getting "SyntaxError: can't assign to operator".

I have no idea what a viable sollution would look like and some help would be greatly appreciated.

HK boy
  • 1,398
  • 11
  • 17
  • 25
  • You can use `eval`. But that's super unrecommended. How about a `Dict[str, dataframe]` instead? – dyz Jun 02 '19 at 17:14

1 Answers1

0

I think maintaining a dictionary of dataframes would be a better solution. Store each dataframe in the dictionary object with a key of userinput. You can easily access any dataframe from the dictionary object later.

dfs = { }
dfs[userinput] = createDF() # add new dataframe
...
print(dfs[userinput]) # access the dataframe
Mezbaul Haque
  • 1,242
  • 9
  • 13