Skip to content Skip to sidebar Skip to footer

Change Column Names In Pandas Dataframe From A List

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

Solution 1:

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.

Solution 2:

You can use this :

df.columns = New_Labels

Solution 3:

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

Solution 4:

df.columns = New_Labels

Take care of the sequence of new column names.

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

Post a Comment for "Change Column Names In Pandas Dataframe From A List"