1

I'm trying to assign values to certain columns at a particular subset of rows in an array using numpy, as follows:

import numpy as np

test = np.zeros((10, 5))
rows = [0, 2, 4, 6, 9]
cols = [1, 2, 3]
proportions = [0.2, 0.3, 0.4]

test[rows, cols] = proportions

However, this returns an error:

IndexError: shape mismatch: indexing arrays could not be broadcast together with shapes (5,) (3,) 

While, when I try to do the same thing in Pandas, it does work:

import pandas as pd

test_df = pd.DataFrame(test)
test_df.iloc[rows, cols] = proportions

print(test_df)

     0    1    2    3    4
0  0.0  0.2  0.3  0.4  0.0
1  0.0  0.0  0.0  0.0  0.0
2  0.0  0.2  0.3  0.4  0.0
3  0.0  0.0  0.0  0.0  0.0
4  0.0  0.2  0.3  0.4  0.0
5  0.0  0.0  0.0  0.0  0.0
6  0.0  0.2  0.3  0.4  0.0
7  0.0  0.0  0.0  0.0  0.0
8  0.0  0.0  0.0  0.0  0.0
9  0.0  0.2  0.3  0.4  0.0

How can I circumvent this issue and make it work in numpy?

0

1 Answer 1

2

Numpy indexing doesn't work quite the same way as pandas .iloc indexing when you want to use the indices of an array (or multiple arrays) as the index. But you can use np.ix_

test[np.ix_(rows, cols)] = proportions
Sign up to request clarification or add additional context in comments.

2 Comments

Amazing, couldn't find anything on this anywhere. Thanks!
This is documented in numpy indexing - under the advanced indexing header: numpy.org/doc/1.18/reference/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.