35

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.

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Antonio López Ruiz
  • 1,396
  • 5
  • 20
  • 36

5 Answers5

67

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)
mgoldwasser
  • 14,558
  • 15
  • 79
  • 103
  • 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! – Antonio López Ruiz Jun 29 '16 at 14:21
  • @AntonioLópezRuiz - ah, got it - I think my edit now answers your question. – mgoldwasser Jun 29 '16 at 14:23
  • 2
    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. – Matthew Arthur Jan 23 '19 at 11:30
  • 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) in ------------> 1 autos1.rename(colunms = {'yearr':'yr' , 'dayas':'days'}, inplace = True):::::::::::::::::::: TypeError: rename() got an unexpected keyword argument 'colunms' – Code Man Oct 28 '21 at 23:59
  • @CodeMan That is because the spelling of `colunms` is wrong. You put `n` before `m`. It should be `columns`! – spectre Mar 18 '22 at 13:18
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)
EdChum
  • 376,765
  • 198
  • 813
  • 562
25

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.

StefanK
  • 2,030
  • 1
  • 21
  • 26
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
HakunaMaData
  • 1,281
  • 12
  • 26
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]: 
william_grisaitis
  • 5,170
  • 3
  • 33
  • 40