How to rename columns by position?

Question:

from pandas import DataFrame
df = DataFrame({
  'A' : [1, 2, 3]
  , 'B' : [3, 2, 1]
})
print(df.rename(columns={'A': 'a', 'B': 'b'}))

I know that I can rename columns like the above.

But sometimes, I just what to rename columns by position so that I don’t have to specify the old names.

df.rename(columns=['a', 'b']))
df.rename(columns={0: 'a', 1: 'b'})

I tried the above. But none of them work. Could anybody show me a concise way to rename without specifying original names?

I looking for a way with minimal code. Ideal, this should and could have been supported by the current function interface of rename(). Using a for-loop or something to create a dictionary with the old column names could be a solution but not preferred.

Asked By: user1424739

||

Answers:

Here you go:

df.rename(columns={ df.columns[0]: 'a', df.columns[1]: 'b',}, inplace = True)
df

Prints:

    a   b
0   1   3
1   2   2
2   3   1
Answered By: sharathnatraj

You might assign directly to pandas.DataFrame.columns i.e.

import pandas as pd
df = pd.DataFrame({'A':[1, 2, 3],'B':[3, 2, 1]})
df.columns = ["X","Y"]
print(df)

output

   X  Y
0  1  3
1  2  2
2  3  1
Answered By: Daweo

With this method, you can rename whatever columns by position. It converts the index position (0, 1) into the column name and create the original mapping ({'A': 'a', 'B': 'b'})

c = {0: 'a', 1: 'b'}
m = dict(zip(df.columns[list(c.keys())], c.values()))
>>> m
{'A': 'a', 'B': 'b'}

>>> df.rename(columns=m)
   a  b
0  1  3
1  2  2
2  3  1
Answered By: Corralien

You can create a function to rename them:

names = iter(['a', 'b'])

def renamer(col):
    return next(names)


df.rename(renamer, axis='columns', inplace=True)

The advantage of this approach is that it is enough that you know the order of the columns to rename and renamer function does not even have to use its parameter.

Answered By: sophros

Unclear if the other solutions work in the presence of two or more columns with the same name. Here’s a function which does:

# rename df columns by position (as opposed to index)
# mapper is a dict where keys = ordinal column position and vals = new titles
# unclear that using the native df rename() function produces the correct results when renaming by position
def rename_df_cols_by_position(df, mapper):
    new_cols = [df.columns[i] if i not in mapper.keys() else mapper[i] for i in range(0, len(df.columns))]
    df.columns = new_cols
    return
Answered By: trance_dude
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.