ENH/BUG: Rename of MultiIndex DataFrames does not work
See original GitHub issuexref #14139 for empty MI
Hi everybody,
in the current version renaming of MultiIndex DataFrames does not work. Lets take the following example:
import datetime as DT
import pandas as pd
df = pd.DataFrame({
'Branch' : 'A A A A A B'.split(),
'Buyer': 'Carl Mark Carl Joe Mark Carl'.split(),
'Quantity': [1,3,5,8,9,3],
'Date' : [
DT.datetime(2013,9,1,13,0),
DT.datetime(2013,9,1,13,5),
DT.datetime(2013,10,1,20,0),
DT.datetime(2013,10,3,10,0),
DT.datetime(2013,12,2,12,0),
DT.datetime(2013,12,2,14,0),
]})
and the following query:
test_df = df[df['Buyer'].isin(['Carl', 'Mark'])].set_index('Buyer', append=True)[['Date']].unstack(['Buyer'])
Now, the following renaming does not work
test_df.rename(columns={('Date', 'Carl'): 'Carl'}, inplace=True)
Thanks in advance
Andy
Issue Analytics
- State:
- Created 10 years ago
- Comments:25 (16 by maintainers)
Top Results From Across the Web
Renaming MultiIndex columns is not working
Try using rename with a level argument - df = df.rename(columns='Sensor {}'.format , level=1). Thanks to Zero for the shorthand improvement.
Read more >MultiIndex / advanced indexing
The rename() method is used to rename the labels of a MultiIndex , and is typically used to rename the columns of a...
Read more >How do I use the MultiIndex in pandas? - YouTube
One of the most powerful features in pandas is multi-level indexing (or "hierarchical indexing"), which allows you to add extra dimensions ...
Read more >Turn Pandas Multi-Index into column
A multi-index dataframe has multi-level, or hierarchical indexing. ... Here, we can see that only indx1 is reset becomes a column, not both ......
Read more >How to Rename Multi index columns in Pandas Dataframe
Pandas dataframe is a powerful two dimensional data structure which allows you to store data in rows... Tagged with python, pandas, ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Just stumbled over this problem. The delicate part is that the function will always return successfully even if
errors="raise"
is passed.Thanks to normanius for the workaround, if a fix is not that easy, maybe the docstring could be extended with a warning?
Just sharing a workaround: renaming of tuples works for flattened indices.