1

I am learning pandas for data cleaning. I am reading one excel file like below.

Excel Image

What I am looking to do is to rename column names like, First Cost Q3 2020, First Cost Q4 2020, First Cost Q1 2021 and so on. There are other column names "AUFC", "First Cost Growth %" and many more. And I also have to make the same like "First Cost".

I am new to pandas and not getting idea How can I rename columns like this. Can any one guide me?

karanrp
  • 230
  • 1
  • 10
  • Welcome to SO, please read [tour] and [mre] and in this case also: [how-to-make-good-reproducible-pandas-examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – Andreas Jul 20 '21 at 11:55
  • hi! Is any one of the answers below working? If so & if you wish, you might consider [accepting](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235) one of them to signal others that the issue is resolved. If not, you can provide feedback so they can be improved (or removed altogether) – Anurag Dabas Jul 27 '21 at 06:42

4 Answers4

2

You can rename a column name by using:

df.rename(columns = {'Q3 2020':'First Cost Q3 2020'}, inplace = True)

To update all column names, you can do this:

df.columns = ['First Cost Q3 2020', 'First Cost Q4 2020', 'First Cost Q1 2021']

1

Try via columns attribute and map() method:

df.columns=df.columns.map(' '.join)

Now If you print df or df.columns you will get your expected output

Anurag Dabas
  • 23,866
  • 9
  • 21
  • 41
0

Simpliest way is to assign the list of desired column names as below (it must be all columns):

df.columns = ['First Cost Q3 2020', 'First Cost Q4 2020', etc].

If Pandas reads this excel as a multilevel columns you will need some more work. Let us know if this is the case

IoaTzimas
  • 10,538
  • 2
  • 13
  • 30
0

You can read the excel file with multiindex columns by adding parameter header=[0,1] as follows (described better here):

df = pd.read_excel(your_path, 
                   header=[0,1], 
                   sheetname=your_sheet_name)

And then later merge the multiindex as described here:

df.columns = df.columns.map(' '.join).str.strip(' ')
Rafa
  • 564
  • 4
  • 12