Skip to content
Advertisement

Using pct_change on grouped multiindex dataframe with a datestamp

I have a following dataframe df:

JavaScript

And I want to know percentage change of users per datestamp device country columns.

I tried:

JavaScript

But it ignores the grouping and checks it simply row by row.

Desired result would look like this:

JavaScript

Advertisement

Answer

It looks like you want the percent change for each device / country combination. And the change reflects year over year. In which case, you don’t want to group by datestamp. Instead, you should sort by datestamp and groupby device and country:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement