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?
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.
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].
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With