cumsum() on multi-index pandas dataframe

2024/10/2 3:17:29

I have a multi-index dataframe that shows the sum of transactions on a monthly frequency.

I am trying to get a cumsum() on yearly basis that respects my 'mapid' and 'service' multi-index. However I don't know how to derive that data

combined_df = combined_df.groupby([pd.Grouper(freq='M'), 'provider', 'mapid', 'service']).sum()cost
datetime   provider mapid   service                  
2017-08-31 Amazon   10147.0 Monitor              0.41Storage             90.51Virtual Machine  11646.32
2017-09-30 Amazon   10147.0 Monitor              0.89Storage            226.06Virtual Machine  32624.91
2017-10-31 Amazon   10147.0 Monitor              0.17Storage            261.72Virtual Machine  36934.93
2017-11-30 Amazon   10147.0 Monitor              0.35Storage            269.06Virtual Machine  30790.70

I would like to derive the follow results

enter image description here

Answer

Group on the last level of your MultiIndex and call DataFrameGroupBy.cumsum:

combined_df['cumsum'] = combined_df.groupby(level=-1)['cost'].cumsum()
https://en.xdnf.cn/q/70889.html

Related Q&A

Python SSL Certification Problems in Tensorflow

Im trying to download the MNIST data which is supposedly handled in: tensorflow.examples.tutorials.mnist.input_data.read_data_sets() As far as Im aware read_data_sets sends a pull request to a server t…

How do I get a python program to run instead of opening in Notepad?

I am having some trouble with opening a .py file. I have a program that calls this .py file (i.e. pathname/example.py file.txt), but instead of running the python program, it opens it in Notepad. How t…

How to find a keys value from a list of dictionaries?

How do I get a given keys value from a list of dictionaries? mylist = [{powerpoint_color: blue,client_name: Sport Parents (Regrouped)},{sort_order: ascending,chart_layout: 1,chart_type: bar} ]The numb…

Wandering star - codeabbey task

Im trying to solve this problem and Im not sure what to do next. Link to the problem Problem statement: Suppose that some preliminary image preprocessing was already done and you have data in form of …

Find delimiter in txt to convert to csv using Python

I have to convert some txt files to csv (and make some operation during the conversion).I use csv.Sniffer() class to detect wich delimiter is used in the txt This codewith open(filename_input, r) as f1…

Assert mocked function called with json string in python

Writing some unit tests in python and using MagicMock to mock out a method that accepts a JSON string as input. In my unit test, I want to assert that it is called with given arguments, however I run i…

read certificate(.crt) and key(.key) file in python

So im using the JIRA-Python module to connect to my companys instance on JIRA and it requires me to pass the certificate and key for this. However using the OpenSSL module,im unable to read my local ce…

Admin FileField current url incorrect

In the Django admin, wherever I have a FileField, there is a "currently" box on the edit page, with a hyperlink to the current file. However, this link is appended to the current page url, an…

Difference between generator expression and generator function

Is there any difference — performance or otherwise — between generator expressions and generator functions?In [1]: def f():...: yield from range(4)...:In [2]: def g():...: return (i for i in…

Django performance testing suite thatll report on metrics (db queries etc.)

I have a complex Django web application that has many person-years of work put into it. It might need optimisation sometime. There are several common operation/flows that I could script with (say) djan…