0

My dataframe is given below. i want to drop rows in two columns which have less than 0 value.

df = 
         name     value1  value2
    0      A      10      10
    1      B     -10      10       #drop
    2      A      10      10
    3      A      40     -10       #drop
    4      C      50      10
    5      C      60      10
    6      D     -70     -10       #drop

I want to drop rows with negative values in value1 and value2 columns. My expected output:

df = 
         name     value1  value2
    0      A      10      10
    1      A      10      10
    2      C      50      10
    3      C      60      10

My present code:

df = df[df['value1','value2']>0]

Output:

KeyError: ('value1','value2')
Mainland
  • 4,110
  • 3
  • 25
  • 56
  • `df[df._get_numeric_data().gt(0).all(1)]`? – Ch3steR Jul 10 '20 at 18:11
  • @Ch3steR I just updated my question to indicate the rows I want to drop. Between, how do I specifically mention those two specific columns? I think your code considers all the numeric columns. – Mainland Jul 10 '20 at 18:14
  • 1
    `df[df[['value1', 'value2']].gt(0).all(1)]`? `gt` is greater than, `ge` greater than equal to – Ch3steR Jul 10 '20 at 18:16
  • Please provide a [mcve], as well as the entire error output. – AMC Jul 10 '20 at 18:17
  • 1
    @AMC I have provided right? – Mainland Jul 10 '20 at 18:17
  • *as well as the entire error output.* is missing. He meant complete error traceback. And while asking question related to Pandas post `df.to_dict()` so users can reproduce the data easily. `pd.read_clipboard()` doesn't work with timeseries and multi indexed df. – Ch3steR Jul 10 '20 at 18:22
  • @AMC Yes I did provide – Mainland Jul 10 '20 at 18:24
  • 1
    @Ch3steR `df[df[['value1', 'value2']].gt(0).all(1)]` This worked. Thanks – Mainland Jul 10 '20 at 18:27
  • Glad to have helped. https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples go through this please. – Ch3steR Jul 10 '20 at 18:29
  • @Mainland _I have provided right?_ No. _Yes I did provide_ No, you didn't. Have you read the page that I linked to? – AMC Jul 10 '20 at 18:34

1 Answers1

1

i guess you mean that if one of the 'value1' or 'value2' are negative, you want to drop the row. so use: df = df[(df['value1'] >= 0) & (df['value2'] >= 0)])

mjrezaee
  • 1,100
  • 5
  • 9
  • what if I want to select five columns out of ten total columns. – Mainland Jul 10 '20 at 18:15
  • I like this code. But, the only problem is, I have to specifically mention the condition specifically to each column. Is there a way by which I can simply denote the names of the columns to be considered? – Mainland Jul 10 '20 at 18:17
  • if you want some column out of total columns, you can first drop negative columns and indicate wanted column from new dataframe. for example if you want 'name' and 'value1' column, you can use `df = df[(df['value1'] >= 0) & (df['value2'] >= 0)])`; `df = df[["name", "value1"]]` – mjrezaee Jul 10 '20 at 18:19