Question Details

No question body available.

Tags

python pandas

Answers (3)

January 18, 2026 Score: 0 Rep: 15,239 Quality: Medium Completeness: 80%

df.rename fails here, because it tries to map labels per level. You can use pd.Index.map with dict.get:

dftest.columns = dftest.columns.map(lambda col: testmap.get(col, col))

Result:

df
test.columns

MultiIndex([('GroupA', 'Current1'), ('GroupA', 'Current2'), ('GroupB', 'Current1'), ('GroupB', 'Metric2')], )

Alternative assignment possible via df.setaxis:

dftest = dftest.setaxis( dftest.columns.map(lambda col: test_map.get(col, col)), axis=1 )
January 18, 2026 Score: 0 Rep: 101 Quality: Low Completeness: 60%

The rename method in pandas generally operates on the labels of specific levels when dealing with a MultiIndex, rather than treating the full column tuples as single keys. Because of this, passing a dictionary of tuples often fails to match the columns as you intend.

To solve this, the most reliable approach is to rebuild the index using a list comprehension or map to apply your dictionary, and then assign it back to df.columns.

When df.rename(columns=...) is called on a MultiIndex, pandas attempts to align the dictionary keys with the labels of the index levels, not the composite tuples (pairs).Since your keys are tuples and the level labels are individual strings, no match is found, and nothing changes.

import pandas as pd

cols = pd.MultiIndex.fromtuples([ ('GroupA', 'Metric1'), ('GroupA', 'Metric2'), ('GroupB', 'Metric1'), ('GroupB', 'Metric2') ]) dftest = pd.DataFrame([ [10, 20, 30, 40], [50, 60, 70, 80] ], columns=cols)

testmap = { ('GroupA', 'Metric1'): ('GroupA', 'Current1'), ('GroupA', 'Metric2'): ('GroupA', 'Current2'), ('GroupB', 'Metric1'): ('GroupB', 'Current1') }

--- Solution ---

Create a new list of columns by looking up each tuple in your map;

if it's not in the map, keep the original tuple.

new
columns = [testmap.get(col, col) for col in dftest.columns]

Assign the new columns back to the DataFrame

dftest.columns = pd.MultiIndex.fromtuples(newcolumns)

print("--- Check Results ---") print(df
test.columns.tolist())
January 18, 2026 Score: 0 Rep: 26 Quality: Low Completeness: 60%

first i run your code there show the same answer the reason is df.rename(columns=testmap) method generally operate individual labels within the index level rather than treating the full column tuple So when you pass a dictionary to renamepandas attempt to match key against the level in the label not the combined towel so it read over the current column check if they exist in your mapping and then assign the new result back df.columns

import pandas as pd

cols = pd.MultiIndex.from
tuples([ ('GroupA', 'Metric1'), ('GroupA', 'Metric2'), ('GroupB', 'Metric1'), ('GroupB', 'Metric2') ])

dftest = pd.DataFrame([ [10, 20, 30, 40], [50, 60, 70, 80] ], columns=cols)

test
map = { ('GroupA', 'Metric1'): ('GroupA', 'Current1'), ('GroupA', 'Metric2'): ('GroupA', 'Current2'), ('GroupB', 'Metric1'): ('GroupB', 'Current1') }

print("--- Original Columns ---") print(dftest.columns.tolist()) #fix

Iterate through columns (tuples), swap if in map, otherwise keep original

new
columns = [testmap.get(col, col) for col in dftest.columns] dftest.columns = pd.MultiIndex.fromtuples(newcolumns)

print("--- Check Results ---") print(df
test.columns.tolist())

The Result :- output

--- Original Columns ---
[('GroupA', 'Metric1'), ('GroupA', 'Metric2'), ('GroupB', 'Metric1'), ('GroupB', 'Metric2')]
--- Check Results ---
[('GroupA', 'Current1'), ('GroupA', 'Current2'), ('GroupB', 'Current1'), ('GroupB', 'Metric2')]