Skip to content

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…

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 shou…