Home > Blockchain >  merge columns in numpy matrix
merge columns in numpy matrix

Time:09-29

I have a NumPy matrix like this one (it could have several columns, this is just an example:

array([[nan, nan],
       [nan, nan],
       ['value 1', nan],
       [nan, nan],
       [nan, nan],
       [nan, 'value 2']], dtype=object)

I need to merge all columns in this matrix, replacing nan values with the corresponding non-nan value (if exists). Example output:

array([[nan],
       [nan],
       ['value 1'],
       [nan],
       [nan],
       ['value 2']], dtype=object)

Is there a way to achieve this with some built-in function in NumPy?

EDIT: if there is more than one non-nan in single row, I will take the first non-nan value.

Values could be string, float or int

CodePudding user response:

Find the rows where the first column is nan. This works because nan!=nan:

rows = arr[:,0] != arr[:,0]

Update the first element of each chosen row with the second element:

arr[rows,0] = arr[rows,1]

Select the first column:

arr[:,[0]]
  • Related