Changing multiple column names but not all of them – Pandas Python

Question:

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.

Answers:

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)
Answered By: EdChum

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
Answered By: HakunaMaData

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)
Answered By: mgoldwasser

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.

Answered By: StefanK

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]: 
Answered By: william_grisaitis
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.