How to replace negative numbers in Pandas Data Frame by zero

0 votes
Is there any way to replace all DataFrame negative numbers by zeros?
Jul 8, 2019 in Python by ana1504.k
• 7,890 points
21,023 views

3 answers to this question.

0 votes

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

In [1]: import pandas as pd

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

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

In [4]: df[df < 0] = 0

In [5]: df
Out[5]: 
   a  b
0  0  0
1  0  2
2  2  1

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

In [1]: import pandas as pd

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

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

In [4]: num = df._get_numeric_data()

In [5]: num[num < 0] = 0

In [6]: df
Out[6]: 
   a  b    c
0  0  0  foo
1  0  2  goo
2  2  1  bar
answered Jul 8, 2019 by SDeb
• 13,250 points
0 votes

Approach:

  • Import pandas module.
  • Create a Dataframe.
  • Check the DataFrame element is less than zero, if yes then assign zero in this element.
  • Display the final DataFrame

 First, let’s create the dataframe.

  • Python3
# importing pandas module

import pandas as pd

  # Creating pandas DataFrame

df = pd.DataFrame({"A": [1, 2, -3, 4, -5, 6],

                   "B": [3, -5, -6, 7, 3, -2],

                   "C": [-4, 5, 6, -7, 5, 4],

                   "D": [34, 5, 32, -3, -56, -54]})

  

# Displaying the original DataFrame

print("Original Array : ")

df

Output :

image

answered Dec 16, 2020 by Gitika
• 65,870 points
0 votes

 If all your columns are numeric, you can use boolean indexing: In [1]: import pandas as pd In [2]: df = pd.DataFrame({'a': [0, -1, 2], 'b': [-3, 2, 1]})  For the more general case, this shows the private method _get_numeric_data: In [1]: import pandas as pd In [2]: df = pd.DataFrame({'a': [0, -1, 2], 'b': [-3, 2, 1], 'c': ['foo', 'goo', 'bar']}) In [3]: df Out[3]: a b c 0 0 -3 foo 1 -1 2 goo 2 2 1 bar In [4]: num = df._get_numeric_data() In [5]: num[num < 0] = 0 In [6]: df Out[6]: a b c 0 0 0 foo 1 0 2 goo 2 2 1 bar

answered Dec 16, 2020 by Rajiv
• 8,880 points

Related Questions In Python

0 votes
1 answer

How to replace entire column in pandas dataframe?

Use the dataframe with respective column names ...READ MORE

answered May 8, 2019 in Python by John
6,402 views
0 votes
1 answer

How to drop missing value in python data-frame?

Hi Sarada,  I understand you are having problems with ...READ MORE

answered May 27, 2019 in Python by sampriti
• 1,100 points
252 views
0 votes
1 answer

How to convert each list item into string in a column of data frame. ?

Hey, To split a string you can use ...READ MORE

answered Feb 5, 2020 in Python by Roshni
• 10,420 points
213 views
0 votes
1 answer

How to append new data in pandas DataFrame?

Hi@MD, You can use the append function to ...READ MORE

answered Jun 22, 2020 in Python by akhtar
• 38,170 points
97 views
0 votes
1 answer

How to replace values with None in Pandas data frame in Python?

Actually in later versions of pandas this ...READ MORE

answered Aug 30, 2018 in Python by Priyaj
• 58,100 points
7,637 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,833 views
0 votes
1 answer

How to read numbers from file in Python?

Assuming you don't have extraneous whitespace: with open('file') ...READ MORE

answered Apr 16, 2019 in Python by SDeb
• 13,250 points
4,380 views
0 votes
1 answer

How to go to top frame in pdb (python debugger)

You can use a PdbExtension for this. See https://github.com/fschulze/pytest-pdb/pull/5 where ...READ MORE

answered Apr 30, 2019 in Python by SDeb
• 13,250 points
396 views