13

Is is possible to change Column Names using data in a list?

df = pd.DataFrame([[1, 1.0, 2.3,0.2,0.53], [2, 3.35, 2.0,0.2,0.65], [2,3.4, 
       2.0,0.25,0.55], [3,3.4,2.0,0.25,0.55], [1,3.4,2.0,0.25,0.55], 
       [3,3.4,2.0,0.25,0.55]], 
       columns=["ID", "A", "B","C","D"])\
       .set_index('ID')

I have my new labels as below:

New_Labels=['NaU', 'MgU', 'AlU', 'SiU']

Is possible to change the names using data in the above list? My original data set has 100 columns and I did not want to do it manually for each column.

I was trying the following using df.rename but keep getting errors. Thanks!

Suresh Raja
  • 735
  • 2
  • 9
  • 19

5 Answers5

14

You can use this :

df.columns = New_Labels
Spandan Brahmbhatt
  • 3,774
  • 6
  • 24
  • 36
9

Using rename is a formally more correct approach. You just have to provide a dictionary that maps your current columns names to the new ones (thing that will guarantee expected results even in case of misplaced columns)

new_names = {'A':'NaU', 'B':'MgU', 'C':'Alu', 'D':'SiU'}
df.rename(index=str, columns=new_names)

Notice you can provide entries for the sole names you want to substitute, the rest will remain the same.

5agado
  • 2,444
  • 2
  • 21
  • 30
6
df = pd.DataFrame([[1, 1.0, 2.3,0.2,0.53], [2, 3.35, 2.0,0.2,0.65], [2,3.4, 
       2.0,0.25,0.55], [3,3.4,2.0,0.25,0.55], [1,3.4,2.0,0.25,0.55], 
       [3,3.4,2.0,0.25,0.55]], 
       columns=["ID", "A", "B","C","D"])\
       .set_index('ID')
New_Labels=['NaU', 'MgU', 'AlU', 'SiU']
df.columns = New_Labels

this will make df look like this:

     NaU  MgU   AlU   SiU
ID                       
1   1.00  2.3  0.20  0.53
2   3.35  2.0  0.20  0.65
2   3.40  2.0  0.25  0.55
3   3.40  2.0  0.25  0.55
1   3.40  2.0  0.25  0.55
3   3.40  2.0  0.25  0.55
jacoblaw
  • 1,263
  • 9
  • 9
5

The accepted rename answer is fine, but it's mainly for mapping old→new names. If we just want to wipe out the column names with a new list, there's no need to create an intermediate mapping dictionary. Just use set_axis directly.


set_axis

To set a list as the columns, use set_axis along axis=1 (the default axis=0 sets the index values):

df.set_axis(New_Labels, axis=1)

#      NaU  MgU   AlU   SiU
# ID                       
# 1   1.00  2.3  0.20  0.53
# 2   3.35  2.0  0.20  0.65
# 2   3.40  2.0  0.25  0.55
# 3   3.40  2.0  0.25  0.55
# 1   3.40  2.0  0.25  0.55
# 3   3.40  2.0  0.25  0.55

Note that set_axis is similar to modifying df.columns directly, but set_axis allows method chaining, e.g.:

df.some_method().set_axis(New_Labels, axis=1).other_method()

Theoretically, set_axis should also provide better error checking than directly modifying an attribute, though I can't find a concrete example at the moment.

tdy
  • 36,675
  • 19
  • 86
  • 83
  • 2
    This should be the accepted answer. It allows you to method chain, which means you don't have to create a temporary `df` to modify the attribute. `.rename` isn't very natural to use as it requires the previous column names. – Denziloe Nov 22 '21 at 13:10
3
df.columns = New_Labels

Take care of the sequence of new column names.

Neo
  • 4,200
  • 5
  • 21
  • 27