I have a two dimensional numpy array, and I’m wanting to
change all of the ‘male’ values to 0 and all of the ‘female’
values to 1.
If I try to assign ‘arr2D[row,element]’ to a specific value,
I get an error.
I tried using the code below, but instead of getting my hoped for
array of ‘[[0,0],[1,1]]’ I got an error. ‘IndexError: arrays used
as indices must be an integer…’.
I don’t know how to determine the index of the element that I’m
looking at in my for loop.
import numpy as np
arr2D = np.array([[‘male’,0],[‘female’,1]])
for row in arr2D:
for element in row:
if element == ‘male’:
arr2D[row,element] = 0
if element == ‘female’
arr2D[row,element] = 1
for row in arr2D:
for element in row:
print(element)
Brandon Harrell is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.