How to replace negative numbers in Pandas Data Frame by zero

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to replace negative numbers in Pandas Data Frame by zero in Python. So Here I am Explain to you all the possible Methods here.

How to replace negative numbers in Pandas Data Frame by zero?

1. How to replace negative numbers in Pandas Data Frame by zero?

Perhaps you could use pandas.where(args) like so:
data_frame = data_frame.where(data_frame < 0, 0)

2. replace negative numbers in Pandas Data Frame by zero

Perhaps you could use pandas.where(args) like so:
data_frame = data_frame.where(data_frame < 0, 0)

Method 1

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

In : import pandas as pd

In : df = pd.DataFrame({'a': [0, -1, 2], 'b': [-3, 2, 1]})

In : df
Out:
a  b
0  0 -3
1 -1  2
2  2  1

In : df[df < 0] = 0

In : df
Out:
a  b
0  0  0
1  0  2
2  2  1

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

In : import pandas as pd

In : df = pd.DataFrame({'a': [0, -1, 2], 'b': [-3, 2, 1],
'c': ['foo', 'goo', 'bar']})

In : df
Out:
a  b    c
0  0 -3  foo
1 -1  2  goo
2  2  1  bar

In : num = df._get_numeric_data()

In : num[num < 0] = 0

In : df
Out:
a  b    c
0  0  0  foo
1  0  2  goo
2  2  1  bar

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

In : import pandas as pd

In : df = pd.DataFrame({'a': pd.to_timedelta([0, -1, 2], 'd'),
...:                    'b': pd.to_timedelta([-3, 2, 1], 'd')})

In : df
Out:
a       b
0  0 days -3 days
1 -1 days  2 days
2  2 days  1 days

In : for k, v in df.iteritems():
...:     v[v < 0] = 0
...:

In : df
Out:
a      b
0 0 days 0 days
1 0 days 2 days
2 2 days 1 days

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

In : import pandas as pd

In : df = pd.DataFrame({'a': pd.to_timedelta([0, -1, 2], 'd'),
...:                    'b': pd.to_timedelta([-3, 2, 1], 'd')})

In : df[df < pd.Timedelta(0)] = 0

In : df
Out:
a      b
0 0 days 0 days
1 0 days 2 days
2 2 days 1 days

Method 2

Perhaps you could use pandas.where(args) like so:

data_frame = data_frame.where(data_frame < 0, 0)

Conclusion

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.