I've just spent several hours trying to get this to work and I'm starting to think I want the impossible, though I'm pretty sure it can be done. I have a pandas dataframe which has a multiindex header (excel spreadsheet, 3 rows header). I am definitely looking at it, so I know it exists, yet when I try to rename a column according to the official documentation, I'm told the column name can't be found.
The table looks like this:
Test | Test1 | Test2
| abc | xyz | abc | xyz
geo1 | geo2 | geo1 | geo2 | geo1 | geo2
------------------------------------------------
a | x | 1 | 0.5 | 1 | 0.5
b | y | 2 | 0.2 | 2 | 0.2
c | z | 3 | 0.4 | 3 | 0.3
I simply want to change "Test" into "Boom", for example. Test is the first value of the column names in level 0, yet it doesn't work. I used one of these:
df.rename(columns={df.columns[0][0]: 'Boom'}, inplace=True, errors='raise')
df.rename(columns={df.columns[0][0]: 'Boom'}, level=0, inplace=True, errors='raise')
df.rename(columns={df.columns.values[0][0]: 'Boom'}, inplace=True, errors='raise')
Problem is, even if I hard code the column names and level, it still doesn't work! This should do the trick as it works in other scripts of mine (2 levels, not 3):
df.rename(columns={'Test': 'Boom'}, level=0, inplace=True, errors='raise')
The error is funny, as it's telling me it can't find the "Test" column (it's literally telling me it can't find the column it just names...). What am I doing wrong??
Thank you all!