4

I have an array in which the first row is a category, the second row is a subcategory, and the third row is a value that I would like to condense.

I'm trying to rearrange array A

[[ 4  4 19 19 20 21 25 26 26 27 27 29]  # category
 [ 1  2  1  2  1  2  1  1  2  1  2  2]  # subcategory
 [ 1  1  3  3  1  2  1  1  1  2  2  2]] # count

into array B

[[ 4 19 20 21 25 26 27 29]  # category
 [ 1  3  1  0  1  1  2  0]  # subcategory 1 count
 [ 1  3  0  2  0  1  2  2]] # subcategory 2 count

I'm as far as this

categories, subcategories = np.unique(A[0], return_counts=True)
B = np.zeros((np.amax(subcategories) + 1, A[0].size))
B[0] = categories

but not sure how to populate the rest. Any ideas?

2 Answers 2

1

This should do the trick:

cat_index = np.searchsorted(categories, A[0])
B[A[1], cat_index] = A[2]
Sign up to request clarification or add additional context in comments.

Comments

1

This should work even when the input isn't sorted:

import numpy as np

A = np.array(
    [[ 4, 4,19,19,20,21,25,26,26,27,27,29],  # category
     [ 1, 2, 1, 2, 1, 2, 1, 1, 2, 1, 2, 2],  # subcategory
     [ 1, 1, 3, 3, 1, 2, 1, 1, 1, 2, 2, 2]]) # count

values, inverse = np.unique(A[0], return_inverse=True)

B = np.zeros((3, len(values)))
B[0] = values
B[1,inverse[A[1] == 1]] = A[2,A[1] == 1]
B[2,inverse[A[1] == 2]] = A[2,A[1] == 2]

Which gives:

[[ 4 19 20 21 25 26 27 29]
 [ 1  3  1  0  1  1  2  0]
 [ 1  3  0  2  0  1  2  2]]

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.