Skip to content
Advertisement

Collapse multiindex after pivot() in pandas pipe

Comming from R/dplyr, I’m used to the piping concept to chain transformation steps during data analysis and have taken this to pandas in a sometimes similar, sometimes better but also sometimes worse fashion (see this article for reference). This is an example of a worse situation.

I’m conducting an analysis of some objects and want to understand the behavior by some grouping variable and for steps further dwon the line (which are not relevant here), I have to have the calculated metrics per grouping in seperate columns. Hence, I’m chaining agg() with pivot() and end up with a multiindex, which I’d like to collapse or flatten.

What I do:

JavaScript

Resulting in:

JavaScript

Expected Ouput as result of a chaining / piping step:

JavaScript

There are stackoverflow-answers that would allow me to collapse the multiindex by breaking the pipe, such as this or this, but I’d like to sustain the pipe since the whole piping process supports the thought-process of data analysis so well.

Advertisement

Answer

DataFrame.pipe

We can flatten the columns without breaking the method chaining by using pipe method and passing in a lambda function that uses set_axis along with MultiIndex.map to flatten the columns:

You can chain the below pipe call after your pivot method

JavaScript

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement