简体   繁体   English

从数据框的python字典创建/获取/提取多个数据框

[英]Creating/Getting/Extracting multiple data frames from python dictionary of dataframes

I have a python dictionary with keys as dataset names and values as the entire data frames themselves, see the dictionary dict below 我有一个python字典,其中键作为数据集名称,值作为整个数据帧本身,请参见下面的字典dict

[Dictionary of Dataframes ] [数据帧字典]

One way id to write all the codes manually like below: 一种手动编写所有代码的方式ID,如下所示:

csv = dict['csv.pkl']
csv_emp = dict['csv_emp.pkl']    
csv_emp_yr= dict['csv_emp_yr.pkl']    
emp_wf=dict['emp_wf.pkl']
emp_yr_wf=dict['emp_yr_wf.pkl']

But this will get very inefficient with more number of datasets. 但是,随着数据集数量的增加,这将变得非常低效。 Any help on how to get this done over a loop? 任何有关如何通过循环完成此操作的帮助吗?

Although I would not recommend this method but you can try this: 尽管我不推荐这种方法,但是您可以尝试以下方法:

import sys
this = sys.modules[__name__] # this is now your current namespace
for key in dict.keys():
    setattr(this, key, dict[key])

Now you can check new variables made with names same as keys of dictionary. 现在,您可以检查名称与字典键相同的新变量。

globals() has risk as it gives you what the namespace is currently pointing to but this can change and so modifying the return from globals() is not a good idea globals()存在风险,因为它会给您提供当前命名空间指向的内容,但是这可能会发生变化,因此修改globals()的返回值不是一个好主意

List can also be used like (limited usecases): 列表也可以像(有限的用例)那样使用:

dataframes = []
for key in dict.keys():
    dataframes.append(dict[key])

Still this is your choice, both of the above methods have some limitations. 仍然是您的选择,以上两种方法都有一些局限性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM