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?

Menno Van Dijk
  • 863
  • 6
  • 24

1 Answers1

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
Bertil Johannes Ipsen
  • 1,656
  • 1
  • 14
  • 27