I am trying to concatenate the values if they have the same indices. I am working with rectangular shape so I know:
- There will always at least 2 of the same indices.
- If there are more than 2 indices, I just need to store the maxs and mins.
Basically,
From:
a = array([
[ 1, 5],
[ 1, 7],
[ 2, 8],
[ 2, 10],
[ 2, 22],
[ 3, 55],
[ 3, 77]])
To:
b = np.array([
[ 1, 5, 7],
[ 2, 8, 22], # [2,8,10,22] but the min is 8 and max is 22
[ 3, 55, 77]])
I have tried to convert it to a list and going through each value using a for loop but it takes a considerable amount of time.
I've also tried sorting the array, np.sort(a, axis=0)
and taking every other row, but since there can be more than two of the indices, it fails.
I am new to numpy, so don't know what else to try.
Any and all suggestion would be helpful, Thank You.
Edit: Its behavior is like a dictionary where the keys are a[0] and values are a[1:]
If there are more than 2 values, I only keep the min and max.