Skip to content
Advertisement

Cumulatively merge rows with the same index

In python pandas, I have a dataframe which looks something like this:

JavaScript

Some of the dates are repeated, with a different count value. I would like to merge these values into one row like this:

JavaScript

If it’s any help the data source is a CSV file. There is likely a way to do this in a for loop but I was wondering if this can be done with a function in pandas? Thanks.

Advertisement

Answer

You can group by the index and sum the values in this case

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