Let’s imagine as a starting point I have a dataframe, similar to the one below (using pd.Index
for the composite columns):
Index, [ATop,AMiddle,ABottom,filt1_1, filt2_1], [ATop,AMiddle,ABottom,filt1_2,filt2_2]
1, Value 1, Value 2
I want to reshape my df
, such that the last 2 levels of the composite columns become columns in their own right. So:
Index, [ATop,AMiddle,ABottom], Filter1, Filter2
1, Value 1, filt1_1, filt2_2,
2, Value 2, filt2_1, filt2_2,
You can see how the [ATop,AMiddle,ABottom]
got extracted into a single column and we ended up with the filter columns to disambiguate the duplicates. The problem I am working on has more than one such groupings, e.g. [BTop,BMiddle,BBottom]
, etc.
I could appreciate this might sound a little weird, but let’s imagine I have a UX grid displayer that wants the data in the latter form and I have my input given as shown above.
Deeply appreciated!