Skip to content
Advertisement

Replacing values greater than a number in pandas dataframe

I have a large dataframe which looks as:

JavaScript

I want to replace each element greater than 9 with 11.

So, the desired output for above example is:

JavaScript

Edit:

My actual dataframe has about 20,000 rows and each row has list of size 2000.

Is there a way to use numpy.minimum function for each row? I assume that it will be faster than list comprehension method?

Advertisement

Answer

You can use apply with list comprehension:

JavaScript

Faster solution is first convert to numpy array and then use numpy.where:

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