Changing a specific column name in pandas DataFrame

PythonPandas

Python Problem Overview


I was looking for an elegant way to change a specified column name in a DataFrame.

play data ...

import pandas as pd
d = {
         'one': [1, 2, 3, 4, 5],
         'two': [9, 8, 7, 6, 5],
         'three': ['a', 'b', 'c', 'd', 'e']
    }
df = pd.DataFrame(d)

The most elegant solution I have found so far ...

names = df.columns.tolist()
names[names.index('two')] = 'new_name'
df.columns = names

I was hoping for a simple one-liner ... this attempt failed ...

df.columns[df.columns.tolist().index('one')] = 'another_name'

Any hints gratefully received.

Python Solutions


Solution 1 - Python

A one liner does exist:

In [27]: df=df.rename(columns = {'two':'new_name'})

In [28]: df
Out[28]: 
  one three  new_name
0    1     a         9
1    2     b         8
2    3     c         7
3    4     d         6
4    5     e         5

Following is the docstring for the rename method.

Definition: df.rename(self, index=None, columns=None, copy=True, inplace=False)
Docstring:
Alter index and / or columns using input function or
functions. Function / dict values must be unique (1-to-1). Labels not
contained in a dict / Series will be left as-is.

Parameters

index : dict-like or function, optional Transformation to apply to index values columns : dict-like or function, optional Transformation to apply to column values copy : boolean, default True Also copy underlying data inplace : boolean, default False Whether to return a new DataFrame. If True then value of copy is ignored.

See also

Series.rename

Returns

renamed : DataFrame (new object)

Solution 2 - Python

Since inplace argument is available, you don't need to copy and assign the original data frame back to itself, but do as follows:

df.rename(columns={'two':'new_name'}, inplace=True)

Solution 3 - Python

What about?

df.columns[2] = "new_name"

Solution 4 - Python

If you know which column # it is (first / second / nth) then this solution posted on a similar question works regardless of whether it is named or unnamed, and in one line: https://stackoverflow.com/a/26336314/4355695

df.rename(columns = {list(df)[1]:'new_name'}, inplace=True)
# 1 is for second column (0,1,2..)

Solution 5 - Python

Pandas 0.21 now has an axis parameter

The rename method has gained an axis parameter to match most of the rest of the pandas API.

So, in addition to this:

df.rename(columns = {'two':'new_name'})

You can do:

df.rename({'two':'new_name'}, axis=1)

or

df.rename({'two':'new_name'}, axis='columns')

Solution 6 - Python

For renaming the columns here is the simple one which will work for both Default(0,1,2,etc;) and existing columns but not much useful for a larger data sets(having many columns).

For a larger data set we can slice the columns that we need and apply the below code:

df.columns = ['new_name','new_name1','old_name']

Solution 7 - Python

pandas version 0.23.4

df.rename(index=str,columns={'old_name':'new_name'},inplace=True)

For the record:

> omitting index=str will give error replace has an unexpected argument > 'columns'

Solution 8 - Python

Following short code can help:

df3 = df3.rename(columns={c: c.replace(' ', '') for c in df3.columns})

Remove spaces from columns.

Solution 9 - Python

Another option would be to simply copy & drop the column:

df = pd.DataFrame(d)
df['new_name'] = df['two']
df = df.drop('two', axis=1)
df.head()

After that you get the result:

    one	three	new_name
0	1	a	    9
1	2	b	    8
2	3	c	    7
3	4	d	    6
4	5	e	    5

Solution 10 - Python

size = 10
df.rename(columns={df.columns[i]: someList[i] for i in range(size)}, inplace = True)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMark GraphView Question on Stackoverflow
Solution 1 - PythonNipun BatraView Answer on Stackoverflow
Solution 2 - PythonJeong-Yoon LeeView Answer on Stackoverflow
Solution 3 - PythonJacob HView Answer on Stackoverflow
Solution 4 - PythonNikhil VJView Answer on Stackoverflow
Solution 5 - PythonTed PetrouView Answer on Stackoverflow
Solution 6 - PythonNaveen ReddyView Answer on Stackoverflow
Solution 7 - PythonKallol MedhiView Answer on Stackoverflow
Solution 8 - PythonEmmanuel MasaboView Answer on Stackoverflow
Solution 9 - PythonankaView Answer on Stackoverflow
Solution 10 - PythonerptocodingView Answer on Stackoverflow