Skip to content
Advertisement

merge columns in numpy matrix

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

JavaScript

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

JavaScript

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

Advertisement

Answer

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

JavaScript

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

JavaScript

Select the first column:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement