How to name Pandas Dataframe Columns automatically?

JChat :

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

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?

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

Note:

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

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

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=16592&siteId=1