使列表中的字符串等于列表中的数据帧



我有一个包含table_names的字符串列表:

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

我也有一个数据框架列表:

list_of_dataframes = [df1, df2, df3 ..]

这些列表长度相等。

我如何遍历两个数据框架并使它们彼此相等?

我设法按如下方式分别循环它们,但希望它们彼此相等,以便df1 = table A..df10 = table J.

For loop I tried

for i in table:
i = l
for l in list_of_dataframes:
l = i

为什么不创建一个字典呢?

你可以这样做:

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

字典是存储这些数据框的更合适的方式:

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

或者,如果你想直接用字母表示:

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

最新更新