Skip to content
Advertisement

How to calculate the Successive Month difference with Groupby in pandas

I have the pandas dataframe,

JavaScript

I need to Group by each id and then apply the monthly difference in each id to get monthly frequency number

I tried out : Solution :

JavaScript

I am expecting the Output dataframe :

JavaScript

Advertisement

Answer

You can use period objects to calculate the number of monthly periods in between 2 dates:

JavaScript

output:

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