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,910 points
44,500 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,300 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,770 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,870 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
13,875 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 28, 2019 in Python by sampriti
• 1,120 points
1,509 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,480 points
923 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 23, 2020 in Python by akhtar
• 38,260 points
757 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,020 points
11,743 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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,449 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,300 points
7,505 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,300 points
2,118 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP