3

I have two arrays. Shown below is a preview of each:

[0.25 0.5  0.75 ... 0.   0.   0.25]
[0.51725357 0.51725357 0.51725357 ... 0.56406901 0.51725357 0.51725357]

You may notice that the second array has values that repeat often. What I would like to do is create two new arrays of the same length. One array would be comprised of all of the unique values from the second array I have listed. The other array would be comprised of the sum of all values from the first array that correspond to a unique value from the second array.

For example, in the arrays I have shown, 0.51725357 has a cumulative sum of 1.75 from the first array, so if 0.51725357 were the first entry in the first of the new arrays, then 1.75 would be the first value in the second of the new arrays.

I understand that I have not worded this well, but hopefully my question is clear enough. Thank you. The usage of any package is fine.

1 Answer 1

5

since you're open to any package, I'd suggest pandas for the groupby functionality:

import pandas as pd

# just for display purposes:
>>> pd.options.display.precision = 10

l1 = [0.25 ,0.5 , 0.75,0.   ,0.   ,0.25]
l2 = [0.51725357 ,0.51725357 ,0.51725357,0.56406901 ,0.51725357, 0.51725357]

>>> pd.DataFrame({"l1": l1, "l2": l2}).groupby("l2").sum()
 
              l1
l2              
0.51725357  1.75
0.56406901  0.00
Sign up to request clarification or add additional context in comments.

1 Comment

Exactly what I was looking for, thank you!

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.