Pandas MultiIndex: split all columns into one column

I have a form data frame of results

  TOTEXPPQ TOTEXPCQ FINLWT21 year quarter 13 1 9.183392e+09 5.459961e+09 1271559.398 2 2.907887e+09 1.834126e+09 481169.672 

and I tried to split all (the first two) columns into the last. My attempt was

 weights = results.pop('FINLWT21') results/weights 

But I get

 ValueError: cannot join with no level specified and no overlapping names 

I don't get: there are overlapping names in the index:

 weights.head() year quarter 13 1 1271559.398 2 481169.672 

Perhaps the best way to make this separation? Do I need to reset the index?

+7
python pandas
source share
1 answer

You must specify the axis to divide (using the div method):

 In [11]: results.div(weights, axis=0) Out[11]: TOTEXPPQ TOTEXPCQ year quarter 13 1 7222.149445 4293.909517 2 6043.371329 3811.807158 

By default, axis = 1 is used, and the result is that the columns and index names of the weights do not overlap, so an error message.

+8
source share

All Articles