Replace negative Numbers in a Pandas DataFrame with Zero

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
5 min

banner

# Table of Contents

  1. Replace negative Numbers in a Pandas DataFrame with Zero
  2. Replace negative Numbers in a Pandas DataFrame with Zero using _get_numeric_data()
  3. Replace negative numbers in a DataFrame with 0 using DataFrame.clip()
  4. Replace negative numbers in a DataFrame with 0 using DataFrame.mask()
  5. Replace negative numbers in a DataFrame with 0 using DataFrame.where()
  6. Replace Timedelta numbers with 0 in a Pandas DataFrame

# Replace negative Numbers in a Pandas DataFrame with Zero

You can use a condition between a square brackets assignment to replace the negative numbers in a Pandas DataFrame with zero.

The condition will only apply the replacement to numbers that are less than 0.

main.py
import pandas as pd df = pd.DataFrame({ 'A': [-1, -2, 0, 1, 2], 'B': [-3, -4, 3, 4, -5] }) print(df) print('-' * 50) df[df < 0] = 0 print(df)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
A B 0 -1 -3 1 -2 -4 2 0 3 3 1 4 4 2 -5 -------------------------------------------------- A B 0 0 0 1 0 0 2 0 3 3 1 4 4 2 0

replace negative numbers in pandas dataframe with zero

We used bracket notation to conditionally replace the negative numbers in the DataFrame with zero.

main.py
df[df < 0] = 0

The condition makes it so the assignment only applies to values that are less than 0.

# Replace negative Numbers in a Pandas DataFrame with Zero using _get_numeric_data()

You can also use the protected DataFrame._get_numeric_data() method to replace the negative numbers in a Pandas DataFrame with zero.

main.py
import pandas as pd df = pd.DataFrame({ 'A': [-1, -2, 0, 1, 2], 'B': [-3, -4, 3, 4, -5] }) print(df) print('-' * 50) numeric_data = df._get_numeric_data() df[numeric_data < 0] = 0 print(df)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
A B 0 -1 -3 1 -2 -4 2 0 3 3 1 4 4 2 -5 -------------------------------------------------- A B 0 0 0 1 0 0 2 0 3 3 1 4 4 2 0

replace negative numbers in dataframe with zero using get numeric data

The DataFrame._get_numeric_data() method is used to only select the numeric data from the DataFrame.

main.py
import pandas as pd df = pd.DataFrame({ 'A': [-1, -2, 0, 1, 2], 'B': [-3, -4, 3, 4, -5] }) # A B # 0 -1 -3 # 1 -2 -4 # 2 0 3 # 3 1 4 # 4 2 -5 print(df._get_numeric_data())

The last step is to replace the numbers in the selected columns that are less than 0.

main.py
df[numeric_data < 0] = 0

This approach is useful when your DataFrame contains non-numeric values as well.

# Replace negative numbers in a DataFrame with 0 using DataFrame.clip()

You can also use the DataFrame.clip() method to replace the negative numbers in a DataFrame with 0.

main.py
import pandas as pd df = pd.DataFrame({ 'A': [-1, -2, 0, 1, 2], 'B': [-3, -4, 3, 4, -5] }) print(df) print('-' * 50) df = df.clip(lower=0) print(df)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
A B 0 -1 -3 1 -2 -4 2 0 3 3 1 4 4 2 -5 -------------------------------------------------- A B 0 0 0 1 0 0 2 0 3 3 1 4 4 2 0

The DataFrame.clip method trims values at a given input threshold.

The lower argument is the minimum threshold value.

All values below the given threshold will be set to it.

You can also use this approach to only clip the numbers in a specific column.

main.py
import pandas as pd df = pd.DataFrame({ 'A': [-1, -2, 0, 1, 2], 'B': [-3, -4, 3, 4, -5] }) print(df) print('-' * 50) df['A'] = df['A'].clip(lower=0) print(df)

Running the code sample produces the following output.

shell
A B 0 -1 -3 1 -2 -4 2 0 3 3 1 4 4 2 -5 -------------------------------------------------- A B 0 0 -3 1 0 -4 2 0 3 3 1 4 4 2 -5

The code sample only replaces the negative numbers in the column "A" with zero.

# Replace negative numbers in a DataFrame with 0 using DataFrame.mask()

You can also use the DataFrame.mask() method to replace the negative numbers in a DataFrame with zero.

main.py
import pandas as pd df = pd.DataFrame({ 'A': [-1, -2, 0, 1, 2], 'B': [-3, -4, 3, 4, -5] }) print(df) print('-' * 50) df = df.mask(df < 0, 0) print(df)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
A B 0 -1 -3 1 -2 -4 2 0 3 3 1 4 4 2 -5 -------------------------------------------------- A B 0 0 0 1 0 0 2 0 3 3 1 4 4 2 0

The DataFrame.mask() method replaces the values where the given condition is met.

main.py
df = df.mask(df < 0, 0)

All values in the DataFrame for which the condition value < 0 returns True get replaced with 0.

# Replace negative numbers in a DataFrame with 0 using DataFrame.where()

You can also use the DataFrame.where() method to replace the negative numbers in a DataFrame with 0.

main.py
import pandas as pd df = pd.DataFrame({ 'A': [-1, -2, 0, 1, 2], 'B': [-3, -4, 3, 4, -5] }) print(df) print('-' * 50) df = df.where(df > 0, 0) print(df)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
A B 0 -1 -3 1 -2 -4 2 0 3 3 1 4 4 2 -5 -------------------------------------------------- A B 0 0 0 1 0 0 2 0 3 3 1 4 4 2 0

The DataFrame.where() method replaces the values where the given condition returns False.

We check if each value is greater than 0, so the condition will return False if the number is less than zero or is equal to 0.

main.py
df = df.where(df > 0, 0)

The method keeps the values where the condition is True.

If the condition is False, the value is replaced by the given replacement (the second argument).

# Replace Timedelta numbers with 0 in a Pandas DataFrame

A similar approach can be used if you need to replace the negative Timedelta numbers in the DataFrame with 0.

main.py
import pandas as pd df = pd.DataFrame({ 'A': pd.to_timedelta([-1, -2, 0, 1, 2], 'd'), 'B': pd.to_timedelta([-3, -4, 3, 4, -5], 'd') }) print(df) df[df < pd.Timedelta(0)] = pd.Timedelta(0) print('-' * 50) print(df)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
A B 0 -1 days -3 days 1 -2 days -4 days 2 0 days 3 days 3 1 days 4 days 4 2 days -5 days -------------------------------------------------- A B 0 0 days 0 days 1 0 days 0 days 2 0 days 3 days 3 1 days 4 days 4 2 days 0 days

replace negative timedelta numbers with 0 in dataframe

We used the pd.Timedelta() class to create a Timedelta object for the comparison.

All objects that have negative Timedelta values get replaced with 0.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.