Categories
Python Answers

How to do conditional replace with Python Pandas?

Spread the love

To do conditional replace with Python Pandas, we can return the items we want to replace and then replace them.

For instance, we write

mask = df.my_channel > 20000
column_name = 'my_channel'
df.loc[mask, column_name] = 0

to replace them items returned by mask, which is the rows with my_channel column values that are bigger than 20000.

Then we use

df.loc[mask, column_name] = 0

to replace the returned rows and columns with .

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *