Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas MultiIndex: Divide all columns by one column

Tags:

python

pandas

I have a data frame results of the form

                  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 was trying to divide all (the first two) columns by the last one. My attempt was

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

But I get

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

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

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

Is there perhaps a better way to do this division? Do I need to reset the index?

like image 227
FooBar Avatar asked Mar 30 '15 19:03

FooBar


People also ask

How do I divide multiple columns in pandas?

Method 2: Pandas divide two columns using div() function It divides the columns elementwise. It accepts a scalar value, series, or dataframe as an argument for dividing with the axis. If the axis is 0 the division is done row-wise and if the axis is 1 then division is done column-wise.

How do I split multiple columns into one column in R?

Data Visualization using R Programming To divide each column by a particular column, we can use division sign (/). For example, if we have a data frame called df that contains three columns say x, y, and z then we can divide all the columns by column z using the command df/df[,3].

Can you Groupby multiple columns in pandas?

How to groupby multiple columns in pandas DataFrame and compute multiple aggregations? groupby() can take the list of columns to group by multiple columns and use the aggregate functions to apply single or multiple aggregations at the same time.


1 Answers

You have to specify the axis for the divide (with 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

The default is axis=1 and the result columns and weights' index names do not overlap, hence the error message.

like image 194
Andy Hayden Avatar answered Sep 19 '22 06:09

Andy Hayden