Skip to content
Advertisement

Add missing rows in pandas DataFrame

I have a DataFrame that looks like this:

JavaScript

What I want to get is:

JavaScript

In short, for each id, add the time rows missing with value 0. How do I do this? I wrote something with a loop, but it’s going to be prohibitively slow for my use case which has several million rows

Advertisement

Answer

Here’s one way using groupby.apply where we use date_range to add the missing times. Then merge it back to df and fill in the missing values of the other columns:

JavaScript

Output:

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