I need to calculate the number of active instances in each month for an ID. I'm able to achieve it with a for loop but I have a very large dataset with 12k ID's and it takes long to complete. Any suggestions for a better solution.
A sample from my data is as shown below
ID instances start_month end_month
key1 x1 1397 1400
key1 x2 1395 1402
key1 x3 1399 1402
key1 x4 1398 1401
key2 x5 1396 1401
key2 x6 1398 1402
key2 x7 1398 1402
I want my output to be like below
key1 1395 1
key1 1396 1
key1 1397 2
key1 1398 3
key1 1399 4
key1 1400 4
key1 1401 3
key1 1402 2
key2 1396 1
key2 1397 1
key2 1398 3
key2 1399 3
key2 1400 3
key2 1401 3
key2 1402 2