In python pandas, I have a dataframe which looks something like this:
> df count date 2021-04-03 23.0 2021-04-04 12.0 2021-04-04 10.0 2021-04-05 42.0 2021-04-06 39.0 ...
Some of the dates are repeated, with a different count value. I would like to merge these values into one row like this:
> df count date 2021-04-03 23.0 2021-04-04 22.0 2021-04-05 42.0 2021-04-06 39.0 ..
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
>>> result = df.groupby(df.index)['count'].sum() >>> result date 2021-04-03 23.0 2021-04-04 22.0 2021-04-05 42.0 2021-04-06 39.0 Name: count, dtype: float64