Skip to content Skip to sidebar Skip to footer

Pandas Concat On Multiindex Columns Based On Level's Name

I am seeing a wierd behavior from pandas, maybe it's just me but I am expecting a different result from what I am getting. so assuming that I have a multi-index dataframe such has:

Solution 1:

I can't think of anything that doesn't manipulate the columns index in some way. But this gets close to what you asked for. Namely, it operates on level name.

ln = 'variables'
pd.concat([df_first.stack(ln), df_sec.stack(ln)]).unstack(ln)

enter image description here

OR

ln = 'ticker'
pd.concat([df_first.stack(ln), df_sec.stack(ln)], axis=1).unstack(ln)

enter image description here

Post a Comment for "Pandas Concat On Multiindex Columns Based On Level's Name"