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')
10
  • df[df._get_numeric_data().gt(0).all(1)]? Commented Jul 10, 2020 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. Commented Jul 10, 2020 at 18:14
  • 1
    df[df[['value1', 'value2']].gt(0).all(1)]? gt is greater than, ge greater than equal to Commented Jul 10, 2020 at 18:16
  • 1
    @AMC I have provided right? Commented Jul 10, 2020 at 18:17
  • 1
    @Ch3steR df[df[['value1', 'value2']].gt(0).all(1)] This worked. Thanks Commented Jul 10, 2020 at 18:27

1 Answer 1

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)])

Sign up to request clarification or add additional context in comments.

3 Comments

what if I want to select five columns out of ten total columns.
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?
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"]]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.