重命名dataFrame名称Python
我想使用变量更改数据框的名称。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我首先说您正在做的事情可能不是一个好习惯。这可能可以通过
dict
来解决。无论如何,我认为
locals()
可以派上用场。如您所见,在循环之前不存在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.As you can see var0, var1 and var2 don't exist before the loop.
You can find more Here!
对于Python中的动态变量名称,请参见您如何动态创建变量?
这样做的方法是制作
字典
,其中键
是名称和名称value
是dataframedb
。不过,我会阻止动态变量名称作为一般规则。
For dynamic variable names in python, see How can you dynamically create variables?
The way to do it is to make a
dictionary
where thekey
is the name and thevalue
is the dataframedb
.I would discourage dynamic variable names as a general rule though.
谢谢大家,解决问题。
最好的,
Thank you all, problem solved.
best,