Last updated: Apr 12, 2024
Reading time·5 min
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
.
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)
Running the code sample produces the following output.
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
We used bracket notation to conditionally replace the negative numbers in the
DataFrame
with zero.
df[df < 0] = 0
The condition makes it so the assignment only applies to values that are less
than 0
.
_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.
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)
Running the code sample produces the following output.
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._get_numeric_data()
method is used to only select the numeric
data from the DataFrame
.
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
.
df[numeric_data < 0] = 0
This approach is useful when your DataFrame
contains non-numeric values as
well.
0
using DataFrame.clip()
You can also use the DataFrame.clip()
method to replace the negative numbers
in a DataFrame
with 0
.
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)
Running the code sample produces the following output.
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.
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.
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.
0
using DataFrame.mask()
You can also use the
DataFrame.mask()
method to replace the negative numbers in a DataFrame
with zero.
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)
Running the code sample produces the following output.
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.
df = df.mask(df < 0, 0)
All values in the DataFrame
for which the condition value < 0
returns True
get replaced with 0
.
0
using DataFrame.where()
You can also use the
DataFrame.where()
method to replace the negative numbers in a DataFrame
with 0
.
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)
Running the code sample produces the following output.
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
.
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).
0
in a Pandas DataFrameA similar approach can be used if you need to replace the negative Timedelta
numbers in the DataFrame
with 0
.
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)
Running the code sample produces the following output.
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
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
.
You can learn more about the related topics by checking out the following tutorials: