Skip to content
Advertisement

Tag: pandas-groupby

Add missing rows in pandas DataFrame

I have a DataFrame that looks like this: What I want to get is: 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 Answer Here’s one way using groupby.apply

How to get grouped cumulative duration in pandas?

I have the following data: id encounter_key datetime 1 111 2019-04-14 1 111 2019-04-14 1 111 2019-07-18 1 122 2019-09-02 2 211 2019-10-03 2 211 2020-10-03 I want to find the cumulative duration, grouped by id and encounter_key to achieve the following: id encounter_key datetime cum_duration_days 1 111 2019-04-14 0 1 111 2019-04-14 0 1 111 2019-07-18 95 1 122

Pandas cumsum with keys

I have two DataFrames (first, second): index_first value_1 value_2 0 100 1 1 200 2 2 300 3 index_second value_1 value_2 0 50 10 1 100 20 2 150 30 Next I concat the two DataFrames with keys: My goal is to calculate the cumulative sum of value_1 and value_2 in z considering the keys. So the final DataFrame should

Advertisement