I have a dataframe with two-level column names as:
ID Value
A B
----------------
1 6
2 5
3 4
4 3
5 2
6 1
I want to change the column head with:
column_mapping = {
('ID', 'A') : 'ID:A',
('Value', 'B'): 'Value:B'
}
I tried rename:
df.rename(columns=column_mapping, inplace=True)
which does not work. Any idea how?
ti7
20.1k8 gold badges50 silver badges87 bronze badges
1 Answer 1
Try:
df.columns = df.columns.map(":".join)
print(df)
Prints:
ID:A Value:B
0 1.0 6.0
1 2.0 5.0
2 3.0 4.0
3 4.0 3.0
4 5.0 2.0
5 6.0 1.0
answered Apr 8, 2024 at 21:30
Andrej Kesely
196k15 gold badges60 silver badges105 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Explore related questions
See similar questions with these tags.
lang-py