40

I would like to know if there is a function to change specific column names but without selecting a specific name or without changing all of them.

I have the code:

df=df.rename(columns = {'nameofacolumn':'newname'})

But with it i have to manually change each one of them writing each name. Also to change all of them I have

df = df.columns['name1','name2','etc']

I would like to have a function to change columns 1 and 3 without writing their names just stating their location.

1
  • You can access the columns by index, by using df.columns[index_num]. Commented Jun 29, 2016 at 13:33

6 Answers 6

74

say you have a dictionary of the new column names and the name of the column they should replace:

df.rename(columns={'old_col':'new_col', 'old_col_2':'new_col_2'}, inplace=True)

But, if you don't have that, and you only have the indices, you can do this:

column_indices = [1,4,5,6]
new_names = ['a','b','c','d']
old_names = df.columns[column_indices]
df.rename(columns=dict(zip(old_names, new_names)), inplace=True)
Sign up to request clarification or add additional context in comments.

5 Comments

In my question I already put that code but said it wasn't what I wanted because I don't want to choose the columns by name but by position. Thanks though!
@AntonioLópezRuiz - ah, got it - I think my edit now answers your question.
For me, using numeric indices to rename columns by position results in the first column name being written across all subsequent indices as well. The approach below using [[ ]] and .values worked for me.
does this approach work for JuPyter notebook as well? because it currently gives me an error saying "got an unexpected keyword argument 'colunms'"........... TypeError-----------Traceback (most recent call last) <ipython-input-17-1d19968693dc> in <module> ------------> 1 autos1.rename(colunms = {'yearr':'yr' , 'dayas':'days'}, inplace = True):::::::::::::::::::: TypeError: rename() got an unexpected keyword argument 'colunms'
@CodeMan That is because the spelling of colunms is wrong. You put n before m. It should be columns!
29

You can use a dict comprehension and pass this to rename:

In [246]:
df = pd.DataFrame(columns=list('abc'))
new_cols=['d','e']
df.rename(columns=dict(zip(df.columns[1:], new_cols)),inplace=True)
df

Out[246]:
Empty DataFrame
Columns: [a, d, e]
Index: []

It also works if you pass a list of ordinal positions:

df.rename(columns=dict(zip(df.columns[[1,2]], new_cols)),inplace=True)

Comments

29

You don't need to use rename method at all.

You simply replace the old column names with new ones using lists. To rename columns 1 and 3 (with index 0 and 2), you do something like this:

df.columns.values[[0, 2]] = ['newname0', 'newname2']

or possibly if you are using older version of pandas than 0.16.0, you do:

df.keys().values[[0, 2]] = ['newname0', 'newname2']

The advantage of this approach is, that you don't need to copy the whole dataframe with syntax df = df.rename, you just change the index values.

Comments

2

You should be able to reference the columns by index using ..df.columns[index]

>> temp = pd.DataFrame(np.random.randn(10, 5),columns=['a', 'b', 'c', 'd', 'e'])
>> print(temp.columns[0])
   a  
>> print(temp.columns[1])
   b

So to change the value of specific columns, first assign the values to an array and change only the values you want

>> newcolumns=temp.columns.values
>> newcolumns[0] = 'New_a'

Assign the new array back to the columns and you'll have what you need

>> temp.columns = newcolumns
>> temp.columns
>> print(temp.columns[0])
   New_a

Comments

1

dict = {'First Name': 'Name', 'Location': 'City', 'Pay': 'Salary'}

call rename () method

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

Comments

0

if you have a dict of {position: new_name}, you can use items()

e.g.,

new_columns = {3: 'fourth_column'}
df.rename(columns={df.columns[i]: new_col for i, new_col in new_cols.items()})

full example:

$ ipython
Python 3.7.10 | packaged by conda-forge | (default, Feb 19 2021, 16:07:37) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.24.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import numpy as np
   ...: import pandas as pd
   ...: 
   ...: rng = np.random.default_rng(seed=0)
   ...: df = pd.DataFrame({key: rng.uniform(size=3) for key in list('abcde')})
   ...: df
Out[1]: 
          a         b         c         d         e
0  0.636962  0.016528  0.606636  0.935072  0.857404
1  0.269787  0.813270  0.729497  0.815854  0.033586
2  0.040974  0.912756  0.543625  0.002739  0.729655

In [2]: new_columns = {3: 'fourth_column'}
   ...: df.rename(columns={df.columns[i]: new_col for i, new_col in new_columns.items()})
Out[2]: 
          a         b         c  fourth_column         e
0  0.636962  0.016528  0.606636       0.935072  0.857404
1  0.269787  0.813270  0.729497       0.815854  0.033586
2  0.040974  0.912756  0.543625       0.002739  0.729655

In [3]: 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.