Home > Mobile >  Getting a string in a list to be equal a dataframe in a list
Getting a string in a list to be equal a dataframe in a list

Time:10-02

I have a list of strings containing table_names:

table = ['table A', 'table B', 'table C'.... 'table J']

I also have a list of dataframes:

list_of_dataframes = [df1, df2 ...]

These lists are of equal length. I would like 'table A' to reference list_of_dataframes[0] but by assigning a variable, I want to have the quotations removed such that 'table A' --> table A (e.g.):

 table A == list_of_dataframes[0]
 table J == list_of_dataframes[9]

How do I do it with a function or through some shortcut without having to do it manually for all the tables?

CodePudding user response:

Why don't you create a dictionary for this?

You can do something like:

for i, item in enumerate(table):
    table_name = item.split()[1]
    dict_of_tables[table_name] = list_of_dataframes[i]

CodePudding user response:

A dictionary is a much more appropriate way to store these dataframes:

>>> dict(zip(table, list_of_dataframes))
# {"table A": df1, "table B": df2, ...}

Or, if you want to refer to them simply by their letter:

>>> dict(zip("".join(table).replace("table ", ""), list_of_dataframes))
# {"A": df1, "B": df2, ...}
  • Related