Skip to content
Advertisement

append or concat a row to a pandas df without collapsing multiindex

I’m trying to add a sum total to an already existing pandas dataframe. The problem is that when I do this my multiindices become just normal indices. How can I prevent that?

JavaScript

neither this

JavaScript

nor this works

JavaScript

Maybe there is also an easier way to add a sum?

Advertisement

Answer

You’re not really creating a MultiIndex dataframe with column_total. You need to add a second level so that the dataframes align:

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