-
-
Notifications
You must be signed in to change notification settings - Fork 17.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add method to flatten all multi-index levels #19950
Comments
having a |
I like your idea of a |
Actually, is this even possible in the use case I described? I mean, I was referring to a multi-index created by |
|
slightly OT: note that |
Looks good! So I guess no problem then? I didn't know about Just to motivate this idea: I love how |
FWIW There is a very popular question related to this on SO, so by that metric it seems like quite a few people could benefit from a solution similar to what is suggested here. |
The answer to the the popular question hints that it's okay to overwrite columns with the result of this method. That quickly got me here, which doesn't work as I expected with version 0.25.1:
Do you consider this expected behavior? I'm not very familiar with selector implementations, but my user intuition tells me this should work. |
Sometimes it is useful to flatten all levels of a multi-index. For example, when pivoting data into a wide format, the new columns are generally multi-indexed. However, when exporting to CSV, sometimes it might be desirable to have only one header row.
AFAIK, there is no dedicated method to flatten an existing multi-index. Assuming I want to combine the multi-index levels with an underscore, I would do this to get one header row:
This is not really obvious, and maybe a dedicated
MultiIndex
method should be added, e.g.df.columns.flatten(sep="_", inplace=False)
. What do you think?The text was updated successfully, but these errors were encountered: