Skip to content
Advertisement

Concatenate columns at the end of a MultiIndex columns DataFrame

Consider the following DataFrames df :

JavaScript

and df1:

JavaScript

I want to concatenate the two DataFrames such that the resulting DataFrame is:

JavaScript

What I run is pandas.concat([df1, df2, axis=1).sort_index(level="kind", axis=1) but that results in

JavaScript

i.e. the column potato is appended at the beginning of df["A"] whereas I want it appended to the end.

Advertisement

Answer

Add parameter sort_remaining=False in DataFrame.sort_index:

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