Se7enSquared
10/22/2019 - 2:30 AM

Numpy: Assign value to a column based on the value of another

bool = array[:, column_for_comparison] == value_for_comparison array[bool, column_for_assignment] = new_value

And all on one line: array[array[:, column_for_comparison] == value_for_comparison, column_for_assignment] = new_value

import numpy as np
arr = np.array[
  ['1', '2', '3', '4', '5', '0']
  ['2', '3', '4', '5', '1', '0']
  ]
# add an additional column filled with zeroes
zeros = np.zeros([arr.shape[0], 1])
arr = np.concatenate([arr, zeros], axis=1)

# if column 3 has a value of 4, assign value 1 to column 5 (the newly added
# column of zeroes)
arr[arr[:, 3] == 4, 5] = 1