Skip to content
Advertisement

I am getting ValueError: invalid literal for int() with base 10 with np.where function

I want to change ‘not available’ value in a df column into 0, and for the rest of the values to change them into integers.

Unique values in the column are:

JavaScript

I run the following code to change values to integers:

JavaScript

I expect that the above would turn all values into integers, yet I get the value error

JavaScript

Any suggestion why the code does not work?

Advertisement

Answer

Before doing

JavaScript

it is neccessary to compute

JavaScript

Latter meaning int version for all which fails, as not available does not make sense as integer, you might avoid this problem by using pandas.Series.apply combined with lambda holding ternary operator as follows

JavaScript

output

JavaScript

This way int is applied only to record which is not equal 'not available'

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