By user1504276

2012-07-05 14:21:15 8 Comments

I have a DataFrame using pandas and column labels that I need to edit to replace the original column labels.

I'd like to change the column names in a DataFrame A where the original column names are:

['$a', '$b', '$c', '$d', '$e'] 


['a', 'b', 'c', 'd', 'e'].

I have the edited column names stored it in a list, but I don't know how to replace the column names.


@Nitish Gaddam 2019-02-15 21:39:15

Import libraries that will help us create a dataframe:

import pandas as pd
import numpy as np

Create Dataframe:

df = pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), columns=['a', 'b', 'c'])

Rename Dataframe:

df.rename(columns={'a':'x', 'b':'y', 'c':'z'}, index=str)

df.columns should now have different values

@Nishant Wattamwar 2019-05-16 12:10:31

df.columns =[needed names of columns]

just add names you want

@vahdet 2019-05-16 12:53:17

How it is more than a shrunk version of the accepted answer?

@goaty 2019-04-24 14:56:34

This is probably the simplest solution to the issue. Its quite self explanatory how it works.

import pandas as pd

df.columns = [['$a', '$b', '$c', '$d', '$e']]
df.rename(columns = {'$a':'a','$b':'b','$c':'c','$d':'d','$e':'e'})


@d_kennetz 2019-04-24 16:29:21

Hi goaty, when posting please explain why your code is a solution to the question. Code only answers are discouraged on SO. Secondly, this question was asked almost 7 years ago and has many tremendous answers. Please add a description of what yours adds that the other answers do not cover already. Thanks

@Kaustubh J 2019-04-11 15:08:57

Assuming you can use regular expression. This solution removes the need of manual encoding using regex

import pandas as pd
import re



@Samuel Liew 2019-04-11 23:49:26

It's good practice on Stack Overflow to add an explanation as to why your solution should work or is better than the existing solutions. For more information read How To Answer.

@Kaustubh J 2019-04-13 13:13:11

Notice how the best-rated answer requires some form of hard coding and the worst rated answer requires only descriptive and procedural approach?

@Amita Rawat 2019-03-29 08:41:44

import pandas as pd

data = {'year':[2015,2011,2007,2003,1999,1996,1992,1987,1983,1979,1975],
        'team':['Australia','India','Australia','Australia','Australia','Sri Lanka','Pakistan','Australia','India','West Indies','West Indies'],
df = pd.DataFrame(data)

#Rename Columns
df.rename(columns={'year':'Years of Win','team':'Winning Team'}, inplace=True)

#Replace format
df.columns = df.columns.str.replace(' ', '_')

@dimension 2018-09-29 08:38:08

Renaming columns while reading the Dataframe:

>>> df = pd.DataFrame({'$a': [1], '$b': [1], '$c': [1]}).rename(columns = 
         {'$a' : 'a','$b':'b','$c':'c'})

   a  b  c
0  1  1  1

@Nursnaaz 2019-01-22 08:18:12

I See 3 easy ways


output : ['$a', '$b', '$c', '$d', '$e']


df.columns = ['a', 'b', 'c', 'd', 'e']


df.columns = df.columns.str.replace('$','')


df.rename(columns={'$a': 'a', '$b': 'b', '$c': 'c', '$d': 'd', '$e' : 'e'}, inplace=True)

@lf2225 2018-02-23 21:47:18

df = df.rename(columns=lambda n: n.replace('$', '')) is a functional way of solving this

@djangoliv 2018-03-09 10:33:18

df.rename(columns=lambda n: n.replace('$', ''), inplace=True)

@M PAUL 2016-03-22 08:59:12

df.columns = ['a', 'b', 'c', 'd', 'e']

It will replace the existing names with the names you provide, in the order you provide.

@llllllllll 2018-05-17 08:53:52

Do not modify df.columns.values, that's wrong.…

@Yog 2018-07-19 04:50:15

@Jeremie 2018-08-29 13:22:40

isn't it df.rename instead?

@Yog 2018-08-29 13:36:10

@JeremieThanks updated

@sbha 2018-07-07 02:07:23

Another option is to rename using a regular expression:

import pandas as pd
import re

df = pd.DataFrame({'$a':[1,2], '$b':[3,4], '$c':[5,6]})

df = df.rename(columns=lambda x: re.sub('\$','',x))
>>> df
   a  b  c
0  1  3  5
1  2  4  6

@seeiespi 2018-04-19 07:48:53

Here's a nifty little function I like to use to cut down on typing:

def rename(data, oldnames, newname): 
    if type(oldnames) == str: #input can be a string or list of strings 
        oldnames = [oldnames] #when renaming multiple columns 
        newname = [newname] #make sure you pass the corresponding list of new names
    i = 0 
    for name in oldnames:
        oldvar = [c for c in data.columns if name in c]
        if len(oldvar) == 0: 
            raise ValueError("Sorry, couldn't find that column in the dataset")
        if len(oldvar) > 1: #doesn't have to be an exact match 
            print("Found multiple columns that matched " + str(name) + " :")
            for c in oldvar:
                print(str(oldvar.index(c)) + ": " + str(c))
            ind = input('please enter the index of the column you would like to rename: ')
            oldvar = oldvar[int(ind)]
        if len(oldvar) == 1:
            oldvar = oldvar[0]
        data = data.rename(columns = {oldvar : newname[i]})
        i += 1 
    return data   

Here is an example of how it works:

In [2]: df = pd.DataFrame(np.random.randint(0,10,size=(10, 4)), columns=['col1','col2','omg','idk'])
#first list = existing variables
#second list = new names for those variables
In [3]: df = rename(df, ['col','omg'],['first','ohmy']) 
Found multiple columns that matched col :
0: col1
1: col2

please enter the index of the column you would like to rename: 0

In [4]: df.columns
Out[5]: Index(['first', 'col2', 'ohmy', 'idk'], dtype='object')

@Sankar guru 2018-05-18 09:02:02

The rename dataframe columns and replace format

import pandas as pd

data = {'year':[2015,2011,2007,2003,1999,1996,1992,1987,1983,1979,1975],
        'team':['Australia','India','Australia','Australia','Australia','Sri Lanka','Pakistan','Australia','India','West Indies','West Indies'],
df = pd.DataFrame(data)

#Rename Columns
df.rename(columns={'year':'Years of Win','team':'Winning Team'}, inplace=True)

#Replace format
df = df.columns.str.replace(' ', '_')

@wolfog 2018-03-17 09:13:08

I think this method is useful:

df.rename(columns={"old_column_name1":"new_column_name1", "old_column_name2":"new_column_name2"})

This method allows you to change column names individually.

@Nodar Okroshiashvili 2018-02-09 07:28:27

Try this. It works for me

df.rename(index=str, columns={"$a": "a", "$b": "b", "$c" : "c", "$d" : "d", "$e" : "e"})

@Ted Petrou 2017-10-24 13:39:15

Pandas 0.21+ Answer

There have been some significant updates to column renaming in version 0.21.

  • The rename method has added the axis parameter which may be set to columns or 1. This update makes this method match the rest of the pandas API. It still has the index and columns parameters but you are no longer forced to use them.
  • The set_axis method with the inplace set to False enables you to rename all the index or column labels with a list.

Examples for Pandas 0.21+

Construct sample DataFrame:

df = pd.DataFrame({'$a':[1,2], '$b': [3,4], 
                   '$c':[5,6], '$d':[7,8], 

   $a  $b  $c  $d  $e
0   1   3   5   7   9
1   2   4   6   8  10

Using rename with axis='columns' or axis=1

df.rename({'$a':'a', '$b':'b', '$c':'c', '$d':'d', '$e':'e'}, axis='columns')


df.rename({'$a':'a', '$b':'b', '$c':'c', '$d':'d', '$e':'e'}, axis=1)

Both result in the following:

   a  b  c  d   e
0  1  3  5  7   9
1  2  4  6  8  10

It is still possible to use the old method signature:

df.rename(columns={'$a':'a', '$b':'b', '$c':'c', '$d':'d', '$e':'e'})

The rename function also accepts functions that will be applied to each column name.

df.rename(lambda x: x[1:], axis='columns')


df.rename(lambda x: x[1:], axis=1)

Using set_axis with a list and inplace=False

You can supply a list to the set_axis method that is equal in length to the number of columns (or index). Currently, inplace defaults to True, but inplace will be defaulted to False in future releases.

df.set_axis(['a', 'b', 'c', 'd', 'e'], axis='columns', inplace=False)


df.set_axis(['a', 'b', 'c', 'd', 'e'], axis=1, inplace=False)

Why not use df.columns = ['a', 'b', 'c', 'd', 'e']?

There is nothing wrong with assigning columns directly like this. It is a perfectly good solution.

The advantage of using set_axis is that it can be used as part of a method chain and that it returns a new copy of the DataFrame. Without it, you would have to store your intermediate steps of the chain to another variable before reassigning the columns.

# new for pandas 0.21+

# old way
df1 = df.some_method1()
df1.columns = columns

@MaxU 2017-11-22 13:27:43

Thank you for the Pandas 0.21+ answer - somehow i missed that part in the "what's new" part...

@Arthur D. Howland 2018-04-04 18:43:56

The solution does not seem to work for Pandas 3.6: df.rename({'$a':'a', '$b':'b', '$c':'c', '$d':'d', '$e':'e'}, axis='columns'). Gets an unexpected keyword argument "axis"

@Nabla 2018-04-13 02:40:01

df.columns = ['a', 'b', 'c', 'd', 'e'] seems not to work anymore, working with version 0.22 I have a warning saying Pandas doesn't allow columns to be created via a new attribute name . how to rename if all my columns are called the same :/

@tommy.carstensen 2018-08-17 12:19:07

Is there a way to rename one, multiple or all columns, if you don't know the name of the column(s) beforehand but just their index? Thanks!

@measureallthethings 2018-12-07 18:32:29

this was a very helpful comment. for example, the lambda function answered my question of how to do the following: (df .groupby(['page',pd.Grouper(key='date',freq='MS')])['clicks'‌​].sum() .unstack(1) .rename(lambda x: x.strftime("%Y-%m"), axis='columns') )

@lexual 2012-07-06 01:48:15

Use the df.rename() function and refer the columns to be renamed. Not all the columns have to be renamed:

df = df.rename(columns={'oldName1': 'newName1', 'oldName2': 'newName2'})
# Or rename the existing DataFrame (rather than creating a copy) 
df.rename(columns={'oldName1': 'newName1', 'oldName2': 'newName2'}, inplace=True)

@darKoram 2012-09-10 22:39:35

when I do this with a 6 column data frame (dataframe <press enter>) the abbreviated representation:code <class 'pandas.core.frame.DataFrame'> Int64Index: 1000 entries, 0 to 999 Data columns: BodyMarkdown 1000 non-null code works, but when i do dataframe.head() the old names for the columns re-appear.

@Monica Heddneck 2016-08-18 19:47:10

I get the dreaded SettingWithCopyWarning: when I use the second code snippet in this answer.

@denfromufa 2016-11-10 17:33:58

is there a version of this with regex replacement?

@denfromufa 2016-11-10 17:39:36

oh, I found it - next answer below!

@vagabond 2017-01-09 22:40:35

@lexual What if two existing columns have the same name ? How do I refer to the old column name?

@irritable_phd_syndrom 2017-07-14 13:24:31

The first solution : df = df.rename(columns={'oldName1': 'newName1', 'oldName2': 'newName2'}) changes the name displayed, but not elements in the underlying data structure. So if you try df['newName1'] you'll get an error. The inplace=True is necessary to avoid that gotchya.

@Mikhail_Sam 2017-12-27 12:04:44

Interesting moment: would using inplace option be faster than reassigning (df = df.rename(...)) ?

@Domnick 2017-11-02 05:17:38

In case you don't want the row names df.columns = ['a', 'b',index=False]

@Domnick 2018-01-12 10:52:33

Dear Downvoters Kindly explain the reason for downvoting it

@Alexander 2016-02-14 00:31:53

df = pd.DataFrame({'$a': [1], '$b': [1], '$c': [1], '$d': [1], '$e': [1]})

If your new list of columns is in the same order as the existing columns, the assignment is simple:

new_cols = ['a', 'b', 'c', 'd', 'e']
df.columns = new_cols
>>> df
   a  b  c  d  e
0  1  1  1  1  1

If you had a dictionary keyed on old column names to new column names, you could do the following:

d = {'$a': 'a', '$b': 'b', '$c': 'c', '$d': 'd', '$e': 'e'}
df.columns = col: d[col])  # Or `.map(d.get)` as pointed out by @PiRSquared.
>>> df
   a  b  c  d  e
0  1  1  1  1  1

If you don't have a list or dictionary mapping, you could strip the leading $ symbol via a list comprehension:

df.columns = [col[1:] if col[0] == '$' else col for col in df]

@piRSquared 2017-09-13 08:48:08

Instead of lambda col: d[col] you could pass d.get... so it would look like

@piRSquared 2017-09-13 08:09:23

One line or Pipeline solutions

I'll focus on two things:

  1. OP clearly states

    I have the edited column names stored it in a list, but I don't know how to replace the column names.

    I do not want to solve the problem of how to replace '$' or strip the first character off of each column header. OP has already done this step. Instead I want to focus on replacing the existing columns object with a new one given a list of replacement column names.

  2. df.columns = new where new is the list of new columns names is as simple as it gets. The drawback of this approach is that it requires editing the existing dataframe's columns attribute and it isn't done inline. I'll show a few ways to perform this via pipelining without editing the existing dataframe.

Setup 1
To focus on the need to rename of replace column names with a pre-existing list, I'll create a new sample dataframe df with initial column names and unrelated new column names.

df = pd.DataFrame({'Jack': [1, 2], 'Mahesh': [3, 4], 'Xin': [5, 6]})
new = ['x098', 'y765', 'z432']


   Jack  Mahesh  Xin
0     1       3    5
1     2       4    6

Solution 1

It has been said already that if you had a dictionary mapping the old column names to new column names, you could use pd.DataFrame.rename.

d = {'Jack': 'x098', 'Mahesh': 'y765', 'Xin': 'z432'}

   x098  y765  z432
0     1     3     5
1     2     4     6

However, you can easily create that dictionary and include it in the call to rename. The following takes advantage of the fact that when iterating over df, we iterate over each column name.

# given just a list of new column names
df.rename(columns=dict(zip(df, new)))

   x098  y765  z432
0     1     3     5
1     2     4     6

This works great if your original column names are unique. But if they are not, then this breaks down.

Setup 2
non-unique columns

df = pd.DataFrame(
    [[1, 3, 5], [2, 4, 6]],
    columns=['Mahesh', 'Mahesh', 'Xin']
new = ['x098', 'y765', 'z432']


   Mahesh  Mahesh  Xin
0       1       3    5
1       2       4    6

Solution 2
pd.concat using the keys argument

First, notice what happens when we attempt to use solution 1:

df.rename(columns=dict(zip(df, new)))

   y765  y765  z432
0     1     3     5
1     2     4     6

We didn't map the new list as the column names. We ended up repeating y765. Instead, we can use the keys argument of the pd.concat function while iterating through the columns of df.

pd.concat([c for _, c in df.items()], axis=1, keys=new) 

   x098  y765  z432
0     1     3     5
1     2     4     6

Solution 3
Reconstruct. This should only be used if you have a single dtype for all columns. Otherwise, you'll end up with dtype object for all columns and converting them back requires more dictionary work.

Single dtype

pd.DataFrame(df.values, df.index, new)

   x098  y765  z432
0     1     3     5
1     2     4     6

Mixed dtype

pd.DataFrame(df.values, df.index, new).astype(dict(zip(new, df.dtypes)))

   x098  y765  z432
0     1     3     5
1     2     4     6

Solution 4
This is a gimmicky trick with transpose and set_index. pd.DataFrame.set_index allows us to set an index inline but there is no corresponding set_columns. So we can transpose, then set_index, and transpose back. However, the same single dtype versus mixed dtype caveat from solution 3 applies here.

Single dtype


   x098  y765  z432
0     1     3     5
1     2     4     6

Mixed dtype

df.T.set_index(np.asarray(new)).T.astype(dict(zip(new, df.dtypes)))

   x098  y765  z432
0     1     3     5
1     2     4     6

Solution 5
Use a lambda in pd.DataFrame.rename that cycles through each element of new
In this solution, we pass a lambda that takes x but then ignores it. It also takes a y but doesn't expect it. Instead, an iterator is given as a default value and I can then use that to cycle through one at a time without regard to what the value of x is.

df.rename(columns=lambda x, y=iter(new): next(y))

   x098  y765  z432
0     1     3     5
1     2     4     6

And as pointed out to me by the folks in sopython chat, if I add a * in between x and y, I can protect my y variable. Though, in this context I don't believe it needs protecting. It is still worth mentioning.

df.rename(columns=lambda x, *, y=iter(new): next(y))

   x098  y765  z432
0     1     3     5
1     2     4     6

@MaxU 2017-09-13 10:21:59

I like "Solution 5" very much... :)

@Scott Boston 2017-09-13 12:46:33

Wow! This is so neat. It really focuses on the internals of a dataframe. Thanks for making me aware.

@WeNYoBen 2017-09-13 14:27:13

Solution 5, awesome ...make me feel I did not know anything about pandas....

@WeNYoBen 2018-10-12 15:59:24

Maybe we can add df.rename(lambda x : x.lstrip('$'),axis=1)

@Josmoor98 2019-05-03 19:19:14

Hi @piRSquared, would you be able to elaborate on how pandas uses the lambda function in Solution 5 please? I don't quite follow what you mean when you say x is ignored?

@kait 2015-11-23 13:56:10

Another way we could replace the original column labels is by stripping the unwanted characters (here '$') from the original column labels.

This could have been done by running a for loop over df.columns and appending the stripped columns to df.columns.

Instead , we can do this neatly in a single statement by using list comprehension like below:

df.columns = [col.strip('$') for col in df.columns]

(strip method in Python strips the given character from beginning and end of the string.)

@Dan Lowe 2015-11-23 14:43:35

Can you explain how/why this works? That will make the answer more valuable for future readers.

@matthhias 2017-06-16 08:27:37

If you have to deal with loads of columns named by the providing system out of your control, I came up with the following approach that is a combination of a general approach and specific replacments in one go.

I first create a dictionary from the dataframe column names using regex expressions in order to throw away certain appendixes of column names and then I add specific replacements to the dictionary to name core columns as expected later in the receiving database.

This is then applied to the dataframe in one go.

df.rename(columns=dict, inplace=True)

@firelynx 2016-09-29 12:30:40

Column names vs Names of Series

I would like to explain a bit what happens behind the scenes.

Dataframes are a set of Series.

Series in turn are an extension of a numpy.array

numpy.arrays have a property .name

This is the name of the series. It is seldom that pandas respects this attribute, but it lingers in places and can be used to hack some pandas behaviors.

Naming the list of columns

A lot of answers here talks about the df.columns attribute being a list when in fact it is a Series. This means it has a .name attribute.

This is what happens if you decide to fill in the name of the columns Series:

df.columns = ['column_one', 'column_two']
df.columns.names = ['name of the list of columns']
df.index.names = ['name of the index']

name of the list of columns     column_one  column_two
name of the index       
0                                    4           1
1                                    5           2
2                                    6           3

Note that the name of the index always comes one column lower.

Artifacts that linger

The .name attribute lingers on sometimes. If you set df.columns = ['one', 'two'] then the will be 'one'.

If you set = 'three' then df.columns will still give you ['one', 'two'], and will give you 'three'


pd.DataFrame( will return

0       1
1       2
2       3

Because pandas reuses the .name of the already defined Series.

Multi level column names

Pandas has ways of doing multi layered column names. There is not so much magic involved but I wanted to cover this in my answer too since I don't see anyone picking up on this here.

    |one            |
    |one      |two  |
0   |  4      |  1  |
1   |  5      |  2  |
2   |  6      |  3  |

This is easily achievable by setting columns to lists, like this:

df.columns = [['one', 'one'], ['one', 'two']]

@andrewwowens 2015-09-01 02:24:17

If you've got the dataframe, df.columns dumps everything into a list you can manipulate and then reassign into your dataframe as the names of columns...

columns = df.columns
columns = [row.replace("$","") for row in columns]
df.rename(columns=dict(zip(columns, things)), inplace=True)
df.head() #to validate the output

Best way? IDK. A way - yes.

A better way of evaluating all the main techniques put forward in the answers to the question is below using cProfile to gage memory & execution time. @kadee, @kaitlyn, & @eumiro had the functions with the fastest execution times - though these functions are so fast we're comparing the rounding of .000 and .001 seconds for all the answers. Moral: my answer above likely isn't the 'Best' way.

import pandas as pd
import cProfile, pstats, re

old_names = ['$a', '$b', '$c', '$d', '$e']
new_names = ['a', 'b', 'c', 'd', 'e']
col_dict = {'$a': 'a', '$b': 'b','$c':'c','$d':'d','$e':'e'}

df = pd.DataFrame({'$a':[1,2], '$b': [10,20],'$c':['bleep','blorp'],'$d':[1,2],'$e':['texa$','']})


def eumiro(df,nn):
    df.columns = nn
    #This direct renaming approach is duplicated in methodology in several other answers: 
    return df

def lexual1(df):
    return df.rename(columns=col_dict)

def lexual2(df,col_dict):
    return df.rename(columns=col_dict, inplace=True)

def Panda_Master_Hayden(df):
    return df.rename(columns=lambda x: x[1:], inplace=True)

def paulo1(df):
    return df.rename(columns=lambda x: x.replace('$', ''))

def paulo2(df):
    return df.rename(columns=lambda x: x.replace('$', ''), inplace=True)

def migloo(df,on,nn):
    return df.rename(columns=dict(zip(on, nn)), inplace=True)

def kadee(df):
    return df.columns.str.replace('$','')

def awo(df):
    columns = df.columns
    columns = [row.replace("$","") for row in columns]
    return df.rename(columns=dict(zip(columns, '')), inplace=True)

def kaitlyn(df):
    df.columns = [col.strip('$') for col in df.columns]
    return df

print 'eumiro''eumiro(df,new_names)')
print 'lexual1''lexual1(df)')
print 'lexual2''lexual2(df,col_dict)')
print 'andy hayden''Panda_Master_Hayden(df)')
print 'paulo1''paulo1(df)')
print 'paulo2''paulo2(df)')
print 'migloo''migloo(df,old_names,new_names)')
print 'kadee''kadee(df)')
print 'awo''awo(df)')
print 'kaitlyn''kaitlyn(df)')

@shantanuo 2015-09-05 13:19:22

Why do you need rename method? Something like this worked for me # df.columns = [row.replace('$', '') for row in df.columns]

@Andrea Ianni ௫ 2016-06-27 11:05:05

I don't understand the 'things' part. What do I have to substitute? The old columns?

@oxer 2016-08-29 21:27:20

Note that these approach do not work for a MultiIndex. For a MultiIndex, you need to do something like the following:

>>> df = pd.DataFrame({('$a','$x'):[1,2], ('$b','$y'): [3,4], ('e','f'):[5,6]})
>>> df
   $a $b  e
   $x $y  f
0  1  3  5
1  2  4  6
>>> rename = {('$a','$x'):('a','x'), ('$b','$y'):('b','y')}
>>> df.columns = pandas.MultiIndex.from_tuples([
        rename.get(item, item) for item in df.columns.tolist()])
>>> df
   a  b  e
   x  y  f
0  1  3  5
1  2  4  6

@Anil_M 2016-08-04 20:26:50

I know this question and answer has been chewed to death. But I referred to it for inspiration for one of the problem I was having . I was able to solve it using bits and pieces from different answers hence providing my response in case anyone needs it.

My method is generic wherein you can add additional delimiters by comma separating delimiters= variable and future-proof it.

Working Code:

import pandas as pd
import re

df = pd.DataFrame({'$a':[1,2], '$b': [3,4],'$c':[5,6], '$d': [7,8], '$e': [9,10]})

delimiters = '$'
matchPattern = '|'.join(map(re.escape, delimiters))
df.columns = [re.split(matchPattern, i)[1] for i in df.columns ]


>>> df
   $a  $b  $c  $d  $e
0   1   3   5   7   9
1   2   4   6   8  10

>>> df
   a  b  c  d   e
0  1  3  5  7   9
1  2  4  6  8  10

@Anton Protopopov 2016-01-28 17:31:39

You could use str.slice for that:

df.columns = df.columns.str.slice(1)

@Thodoris P 2015-11-29 19:22:47

Real simple just use

df.columns = ['Name1', 'Name2', 'Name3'...]

and it will assign the column names by the order you put them

@kadee 2015-05-30 13:24:05

As documented in

df.columns = df.columns.str.replace('$','')

@jorijnsmit 2018-08-25 10:09:09

By far the most efficient and readable solution.

@migloo 2015-05-21 17:48:33

old_names = ['$a', '$b', '$c', '$d', '$e'] 
new_names = ['a', 'b', 'c', 'd', 'e']
df.rename(columns=dict(zip(old_names, new_names)), inplace=True)

This way you can manually edit the new_names as you wish. Works great when you need to rename only a few columns to correct mispellings, accents, remove special characters etc.

@Christopher Pearson 2015-06-22 22:05:08

I like this approach, but I think df.columns = ['a', 'b', 'c', 'd', 'e'] is simpler.

@bkowshik 2015-07-20 07:18:33

I like this method of zipping old and new names. We can use df.columns.values to get the old names.

@mythicalcoder 2016-10-27 13:59:44

I display the tabular view and copy the columns to old_names. I copy the requirement array to new_names. Then use dict(zip(old_names, new_names)) Very elegant solution.

@Tim Gottgetreu 2017-07-12 23:12:29

I often use subsets of lists from something like: myList = list(df) myList[10:20] , etc - so this is perfect.

Related Questions

Sponsored Content

16 Answered Questions

[SOLVED] Selecting multiple columns in a pandas dataframe

34 Answered Questions

[SOLVED] How do I sort a dictionary by value?

18 Answered Questions

[SOLVED] Get list from pandas DataFrame column headers

17 Answered Questions

[SOLVED] How to iterate over rows in a DataFrame in Pandas?

14 Answered Questions

[SOLVED] Select rows from a DataFrame based on values in a column in pandas

23 Answered Questions

[SOLVED] Adding new column to existing DataFrame in Python pandas

13 Answered Questions

[SOLVED] "Large data" work flows using pandas

1 Answered Questions

[SOLVED] Use a list of values to select rows from a pandas dataframe

7 Answered Questions

[SOLVED] Change data type of columns in Pandas

26 Answered Questions

[SOLVED] How to print without newline or space?

  • 2009-01-29 20:58:25
  • Andrea Ambu
  • 1675777 View
  • 1625 Score
  • 26 Answer
  • Tags:   python newline

Sponsored Content