如何自动命名 Pandas 数据框列? [英] How to name Pandas Dataframe Columns automatically?

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

问题描述

我有一个带有 102 列的 Pandas 数据框 df.每列的名称都不同,例如 A, B, C 等,以提供以下结构的原始数据帧

I have a Pandas dataframe df with 102 columns. Each column is named differently, say A, B, C etc. to give the original dataframe following structure

         Column A.    Column B.  Column C.   ....
Row 1.    
Row 2.
---
Row n

我想将列名称从 A, B, C 等更改为 F1, F2, F3, ...., F102.我尝试使用 df.columns 但没有成功地以这种方式重命名它们.有什么简单的方法可以自动将所有列名自动重命名为 F1 到 F102,而不是单独重命名每个列名?

I would like to change the columns names from A, B, C etc. to F1, F2, F3, ...., F102. I tried using df.columns but wasn't successful in renaming them this way. Any simple way to automatically rename all column names to F1 to F102 automatically, insteading of renaming each column name individually?

推荐答案

df.columns=["F"+str(i) for i in range(1, 103)]

<小时>

注意:

您可以使用计算出的列数 (+ 1) 代替神奇"数字 103,例如

Instead of a "magic" number 103 you may use the calculated number of columns (+ 1), e.g.

  • len(df.columns) + 1
  • df.shape[1] + 1.

(感谢 ALollz 在他的评论中提供了这个提示.)

(Thanks to ALollz for this tip in his comment.)

这篇关于如何自动命名 Pandas 数据框列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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