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]
2
  • Similar question stackoverflow.com/questions/4373631/… Commented Mar 12, 2016 at 18:11
  • 1
    That doesn't look like a valid NumPy array format. Also, are the dates sorted in that sequence? Commented Mar 12, 2016 at 18:16

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.