0

I would like to create a new pandas dataframe from an existing dataframe with only rows where specific columns values are bigger than x. For example:

df =   A B C
     0 1 0 2
     1 2 1 3
     2 3 3 0
     3 4 2 5

I want to create new_df with only the rows where the values of columns B and C are bigger than 0 which means

new_df =  A B C
        0 2 1 3
        1 4 2 5

I used the command :

new_df = df.loc[df['B'] > 0 & df['C'] > 0]

but it doesn't work

1
  • 1
    df.loc[df.B.gt(0) & df.C.gt(0), :] ? Commented Aug 1, 2020 at 19:35

2 Answers 2

3

You almost had it. Use parenthesis to enclose conditions:

new_df = df.loc[(df['B'] > 0) & (df['C'] > 0)]

The binary operators & and | take precedence over equalities/inequalities. So what you were doing was equivalent to:

new_df = df.loc[(df['B'] > (0 & df['C'])) > 0]
Sign up to request clarification or add additional context in comments.

Comments

1

You can use this:

df[(df[['B', 'C']] > 0).all(axis=1)]

Output:

   A  B  C
1  2  1  3
3  4  2  5

Comments

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.