重命名 pandas 中的列

Renaming columns in pandas

有人可以帮我重命名列吗?我有一个包含大约 150 列的大型数据集,我需要重命名每一列。在过去的 2 个小时里,我一直在寻找答案,但我找不到任何东西。我知道如何使用基本功能 'rename',但我很确定有比一一重命名每一列更简单快捷的方法吗?任何帮助将非常感激!谢谢:)

直接重命名即可,如:

df.columns = ['first', 'second', 'third']

在你的情况下你可以使用:

df.columns = ['Special{i}'.format(i=i) for i in range(0, len(df.columns))]

尝试:

df.columns = 'Special' + pd.RangeIndex(1, len(df.columns)+1).astype(str)
print(df.columns)

# Output
Index(['Special1', 'Special2', 'Special3', 'Special4', 'Special5', 'Special6',
       'Special7'],
      dtype='object')