Skip to content
Advertisement

How to replace negative numbers in Pandas Data Frame by zero

I would like to know if there is someway of replacing all DataFrame negative numbers by zeros?

Advertisement

Answer

If all your columns are numeric, you can use boolean indexing:

JavaScript

For the more general case, this answer shows the private method _get_numeric_data:

JavaScript

With timedelta type, boolean indexing seems to work on separate columns, but not on the whole dataframe. So you can do:

JavaScript

Update: comparison with a pd.Timedelta works on the whole DataFrame:

JavaScript
Advertisement