I have something similar to this
df = pd.DataFrame(np.random.randint(2, 10, size = (5, 2)))
df.index = pd.MultiIndex.from_tuples([(1, 'A'), (2, 'A'), (4, 'B'), (5, 'B'), (8, 'B')])
df.index.names = ['foo', 'bar']
df.columns = ['count1', 'count2']
df
which gives:
count1 count2
foo bar
1 A 6 7
2 A 2 9
4 B 6 7
5 B 4 6
8 B 5 6
I also have a list of totals -obtained from somewhere else- by the same 'foo' index:
totals = pd.DataFrame([2., 1., 1., 1., 10.])
totals.index = [1, 2, 4, 5, 8]
totals.index.names = ['foo']
totals
which gives:
0
foo
1 2
2 1
4 1
5 1
8 10
How can I divide all the columns of df (count1 and count2) by the the foo number that is in totals? (hence, i need to match by the 'foo' number)
I checked this question, which looks like it should do the trick, but I couldn't figure it out.
I tried
df.div(totals, axis = 0)
and changing the level option in div, but no success.
As always, thanks a lot for your time