Pandas division (.div) with multiindex

2024/10/11 12:26:50

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

Answer

try:

df.div(totals[0],axis='index',level='foo')count1  count2
foo bar                
1   A       1.0     4.5
2   A       4.0     8.0
4   B       5.0     9.0
5   B       5.0     5.0
8   B       0.9     0.5

also:

totals = pd.DataFrame([2., 1., 1., 1., 10.])
totals.index = [[1, 2, 4, 5, 8],['A', 'A', 'B', 'A', 'B']]
totals.index.names = ['foo','bar']
totals0
foo bar      
1   A     2.0
2   A     1.0
4   B     1.0
5   A     1.0
8   B    10.0df[['count1','count2']].div(totals[0],axis='index')count1  count2
foo bar                
1   A       1.0     4.5
2   A       4.0     8.0
4   B       5.0     9.0
5   A       NaN     NaNB       NaN     NaN
8   B       0.9     0.5
https://en.xdnf.cn/q/69773.html

Related Q&A

Add a delay to a specific scrapy Request

Is it possible to delay the retry of a particular scrapy Request. I have a middleware which needs to defer the request of a page until a later time. I know how to do the basic deferal (end of queue), a…

importing without executing the class - python

my problem is about i have a file that contain class and inside this class there is bunch of code will be executed so whenever i import that file it will executed ! without creating an object of the…

If a command line program is unsure of stdouts encoding, what encoding should it output?

I have a command line program written in Python, and when I pipe it through another program on the command line, sys.stdout.encoding is None. This makes sense, I suppose -- the output could be another…

How to generate JSON-API data attribute vs results attribute in Django Rest Framework JSON API?

I have a django 1.9.2 project using Django Rest Framework JSON API:https://github.com/django-json-api/django-rest-framework-json-api:My viewset looks like this:class QuestionViewSet(viewsets.ReadOnlyMo…

How connect my GoPro Hero 4 camera live stream to openCV using Python?

I m having troubles trying to capture a live stream from my new GoPro Hero 4 camera and do some image processing on it using openCV.Here is my trial (nothing shows up on the created windowimport cv2 im…

I thought Python passed everything by reference?

Take the following code#module functions.py def foo(input, new_val):input = new_val#module main.py input = 5 functions.foo(input, 10)print inputI thought input would now be 10. Why is this not the cas…

Python: -mno -cygwin

im trying to learn a lot of python on windows and that includes installing several packages, however everytime i invoke python setup.py install i have a problem with -mno -cygwin for gcc. ive have rea…

Django Sites Framework initial setup

Im comfortable with fairly one-dimensional Django implementations, but now trying to understand the multi-sites-with-shared-stuff process. Ive read through the Django Sites Framework and many posts o…

Data corruption: Wheres the bug‽

Last edit: Ive figured out what the problem was (see my own answer below) but I cannot mark the question as answered, it would seem. If someone can answer the questions I have in my answer below, name…

Python NetworkX — set node color automatically based on a list of values

I generated a graph with networkx import networkx as nx s = 5 G = nx.grid_graph(dim=[s,s]) nodes = list(G.nodes) edges = list(G.edges) p = [] for i in range(0, s):for j in range(0, s):p.append([i,j])…