14

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!

1
  • df.columns[1:] = New_Labels ? Commented Aug 2, 2017 at 18:42

5 Answers 5

20

You can use this :

df.columns = New_Labels 
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

7

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.

1 Comment

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.
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 

Comments

4
df.columns = New_Labels 

Take care of the sequence of new column names.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.