Skip to content
Advertisement

How to prevent np.where from turning 0 into ‘0’?

I want to create an array with np.where that has strings and 0s in it. So usually its dtype would be ‘object’. Minimal example:

JavaScript

As a result I get

JavaScript

I would like those ‘0’s to be 0s. Since np.where has no argument for dtype, I don’t know how to do this except by replacing them afterwards. There has to be a better way to do this.

Advertisement

Answer

You could use an object array as first input values for where:

JavaScript

Output:

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