在Python中动态重命名数据框 [英] dynamically rename data frame in Python

查看:376
本文介绍了在Python中动态重命名数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试动态创建数据集:

I'm trying to dynamically create datasets:

def CreateDF (indsn, outdsn):
    outdsn = pd.DataFrame(indsn)
    ....
    return outdsn

CreateDF (phase_df, phase_df2)

但是它不起作用,出现以下错误:

But it does not work, I get the following error:


NameError:name未定义'phase_df2'

NameError: name 'phase_df2' is not defined

能够执行上述操作可能会很有用,因为我还可以遍历数据集列表(对于列表中的每个数据集),但我不确定如何动态重命名它们。

Being able to do the above could be useful because I could also loop through a list of datasets (for each datasets in the list), but I'm not sure how to dynamically rename them.

推荐答案

这有点稀疏,因为您没有提供有关为什么需要发生这种情况或您的原始资料看起来如何的信息,但是总体思路是:

It's a little sparse because you didn't give us info on why this needs to happen, or what your source material looks like, but the general idea is:

def make_df(name):
    ...
    return df

dict_of_dfs = dict()
df_names = []        #a list of all the dataframes you want to create

for name in df_names:
    dict_of_dfs[name] = make_df(name)

现在不是变量,而是可以将字典条目命名为键,并且每个值都是数据框。

Now instead of variables, you can name dictionary entries as keys and each value will be the dataframe.

这篇关于在Python中动态重命名数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆