I want to set all column names at one line. How should I do it? I tried many things but couldn't do it, including renaming columns.
Asked
Active
Viewed 69 times
0
-
Or `droplevel` of you rather want to remove one level – mozway Dec 15 '22 at 18:00
1 Answers
0
You need to flatten your multiindex column header.
df.columns = df.columns.map('_'.join)
Or using f-string with list comprehension:
df.columns = [f'{i}_{j}' if j else f'{i}' for i, j in df.columns]

Scott Boston
- 147,308
- 15
- 139
- 187