重命名dataFrame名称Python

发布于 2025-02-12 07:20:18 字数 233 浏览 3 评论 0原文

我想使用变量更改数据框的名称。


db = pd.Dataframe()

sopa = ['cebola','batata']


for sopa_id in sopa: 

    sopa_id = db

我希望输出是:

 cebola = db

 batata = db

目标是每个变量获取数据框架名称。

I want to change the name of an dataframe using variables.


db = pd.Dataframe()

sopa = ['cebola','batata']


for sopa_id in sopa: 

    sopa_id = db

I want that the output to be:

 cebola = db

 batata = db

The goal is that each one of the variable get the dataframe name.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

极度宠爱 2025-02-19 07:20:19

我首先说您正在做的事情可能不是一个好习惯。这可能可以通过dict来解决。

无论如何,我认为locals()可以派上用场。

for i in range(3):
    locals()["var"+str(i)] = i
    
print(var0)
print(var1)
print(var2)

如您所见,在循环之前不存在VAR0,VAR1和VAR2。
您可以找到更多在这里

I start by saying that what you are doing is probably not good practice. This can probably be solved with dict.

Anyway I think locals() can come in handy.

for i in range(3):
    locals()["var"+str(i)] = i
    
print(var0)
print(var1)
print(var2)

As you can see var0, var1 and var2 don't exist before the loop.
You can find more Here!

谁的新欢旧爱 2025-02-19 07:20:19

对于Python中的动态变量名称,请参见您如何动态创建变量?

这样做的方法是制作字典,其中是名称和名称value是dataframe db
不过,我会阻止动态变量名称作为一般规则。

db = pd.DataFrame()
# -- Do whatever here to populate --
sopa = ['cebola','batata']

dataframeDict = {}
for sopa_id in sopa:
    dataframeDict[sopa_id] = db

For dynamic variable names in python, see How can you dynamically create variables?

The way to do it is to make a dictionary where the key is the name and the value is the dataframe db.
I would discourage dynamic variable names as a general rule though.

db = pd.DataFrame()
# -- Do whatever here to populate --
sopa = ['cebola','batata']

dataframeDict = {}
for sopa_id in sopa:
    dataframeDict[sopa_id] = db
别靠近我心 2025-02-19 07:20:19

谢谢大家,解决问题。

db = pd.Dataframe()

sopa = ['cebola','batata']


for sopa_id in sopa: 
    locals()["new_name_is_"+str(sopa_id)] = db

最好的,

Thank you all, problem solved.

db = pd.Dataframe()

sopa = ['cebola','batata']


for sopa_id in sopa: 
    locals()["new_name_is_"+str(sopa_id)] = db

best,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文