【问题标题】:How to rename many dfs with digit-like column?如何用类似数字的列重命名许多 dfs?
【发布时间】:2020-09-17 07:36:27
【问题描述】:

我有 5 个 dfs 调用: control1,control2,control3,control4,control5

所有这些都有一个column name 1。此列名称为int 类型。

如何将每个 df 的列名更改为 'CODIGO'str 类型)。

我试过这个:

for j in range(1,6):
   exec(f"control{j}=control{j}.rename(columns=lambda x: x.replace(1, 'CODIGO'))")

但不起作用。你能帮帮我吗?

【问题讨论】:

  • 您应该使用字典或列表来存储您的数据帧。但是您可能应该将1 更改为"1",因为您要替换的是字符串,而不是int。如果您能详细说明为什么这种方法“不起作用”,那也会有所帮助。

标签: python pandas rename


【解决方案1】:

像这样:

l = [control1, control2, control3, control4, control5]

for df in l:
    df.rename(columns={1:'CODIGO'}, inplace=True)

【讨论】:

    猜你喜欢
    • 2020-09-20
    • 1970-01-01
    • 2021-08-14
    • 2014-05-02
    • 2023-01-02
    • 2023-01-10
    • 2020-05-15
    • 2016-10-08
    • 1970-01-01
    相关资源
    最近更新 更多