2

I have a numpy array:

   [2016-10-20 00:00:00, 6, 17],
   [2016-10-20 00:00:00, 0, 21],
   [2017-09-11 00:00:00, 7, 22],
   [2017-09-11 00:00:00, 5, 30],
   [2017-09-11 00:00:00, 2, 40]

Is there an easy way to merge the the rows with the same dates and sum up the values belonging to the same date?

For the example above, It should look like this:

   [2016-10-20 00:00:00, 6, 38],
   [2017-09-11 00:00:00, 14, 92]
Lanza
  • 587
  • 1
  • 5
  • 22

1 Answers1

4

I suggest using pandas for this:

import pandas as pd

d = pd.DataFrame([
    ['2016-10-20 00:00:00', 6, 17],
    ['2016-10-20 00:00:00', 0, 21],
    ['2017-09-11 00:00:00', 7, 22],
    ['2017-09-11 00:00:00', 5, 30],
    ['2017-09-11 00:00:00', 2, 40]
])

d.groupby([0]).aggregate(sum)

#                       1   2
# 0
# 2016-10-20 00:00:00   6  38
# 2017-09-11 00:00:00  14  92

Note that pandas.DataFrame accepts a numpy array as input.

ChrisP
  • 5,812
  • 1
  • 33
  • 36