将数据框列表与字符串列表相关联的Pythonic方法

将数据框列表与字符串列表相关联的Pythonic方法

本文介绍了将数据框列表与字符串列表相关联的Pythonic方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将json对象转换为csv时遇到此问题:

In encountered this problem when converting a json object to a csv:

我现在有两个列表:

list_A是字符串列表.每个字符串都是df的名称.

list_A is a list of strings. Each string is a name of df.

list_A = ['df1', 'df2', 'df3']

list_B具有3个pandas.core.frame.DataFrame对象.

list_B has 3 pandas.core.frame.DataFrame objects.

list_B[0] = [an entire df with columns, rows etc]

什么代码可以确保一个列表中的字符串与另一个列表中的数据帧之间的关联,例如df1 = list_B [0],然后df2 = list_B [1],依此类推?

What code would ensure association between strings from the one list with the dataframes in the other, such as df1 = list_B[0] then df2 = list_B[1] and so on?

谢谢

推荐答案

要么使用字典,要么

dfs = dict(zip(list_A, list_B))

然后使用dfs['df1']访问单个数据帧:

then access individual dataframes with dfs['df1']:

list_a = ['a', 'b', 'c']
list_b = [1, 2, 3]
d = dict(zip(list_a, list_b))
print(d['a'])
# 1

或破解locals:

for name, df in zip(list_A, list_B):
    locals()[name] = df

然后,您可以直接访问单个数据帧,df1df2等:

Then you can access individual dataframes directly, df1, df2 etc:

list_a = ['a', 'b', 'c']
list_b = [1, 2, 3]
for key, value in zip(list_a, list_b):
    locals()[key] = value
print(a)
#  1

这篇关于将数据框列表与字符串列表相关联的Pythonic方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 14:19