1

I have two data frames that have same length like this

df1: 
   density
1  1,45
2  3,87
3  4,35
4  2,87
5  0.74
6  9.34
7  3.087
8  0.28
9  6,47
10 5,59

The second data frame looks like this

df2:
  State
1  1
2  1
3  1
4  1
5  1
6  1
7  0
8  0
9  0
10 0

I want an output that looks like this which means filter df1 in order to keep only values where df2 is equal to 1 :

output: 
   density
1  1,45
2  3,87
3  4,35
4  2,87
5  0.74
6  9.34

How can I do that? Can you help me please.

1
  • 4
    df1[df2.eq(1).values] ? Commented Apr 12, 2018 at 19:43

2 Answers 2

5

Let's use boolean index:

df1[df2.eq(1).values]

Output:

  density
1    1,45
2    3,87
3    4,35
4    2,87
5    0.74
6    9.34
Sign up to request clarification or add additional context in comments.

1 Comment

@piRSquared Still confused which is my answer and which is Scott's
5

This should work

df1[df2.State.astype(bool)]

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.