0

I retrieve several dataframes from spreadsheets of an Excel file.

I would like to store these dataframes into a list so that I can concatenate the dataframes into one dataframe.

However, how can I store the variables themselves instead of their them.

  • These are the data frames that I created.
df0120
df0220
df0320
df0419
df0519
df0619
df0719
df0819
df0919
df_lst = list()
for name in dir():
    if name.startswith('df0'):
      df_lst.append(name)
print(df_lst)
  • My results
['df0120', 'df0220'...]
  • Expected results
[df0120, df0220 ...]
James Huang
  • 63
  • 1
  • 10
  • 1
    well you can't really store variable names directly, you have to store them in some file like a json file and then read them from your script – Souames Nov 17 '20 at 23:19
  • 2
    `[df0120, df0220]` is not a thing that can exist in Python. `'df0120'` is a *string* which will appear as `df0120` when you *print* it. But in source code or as part of a list it will be represented with quotes because that is the syntax of Python. – mkrieger1 Nov 17 '20 at 23:19

1 Answers1

1

What you see is how Python prints a list of strings in the built-in way, by itself. But, you can print it yourself in another way if you want:

print('['+', '.join(df_lst)+']')
tevemadar
  • 12,389
  • 3
  • 21
  • 49
  • Could I ask what's '+' for? – James Huang Nov 18 '20 at 11:30
  • @JamesHuang that's about putting strings after each other (*concatenation* is the technical term for it). Like `print('Hello'+' '+'World!')` will print `Hello World!`, the expression above also puts together three strings, a `'['` at the beginning, a `']'` at the end, and the result of `', '.join(...)` between them. If you wonder why `join()` looks like as it does, this topic may be interesting: https://stackoverflow.com/questions/493819/why-is-it-string-joinlist-instead-of-list-joinstring – tevemadar Nov 18 '20 at 12:31
  • Thank you, this helps a lot. – James Huang Nov 18 '20 at 17:13