[英]Pandas - Filter data frame with another data frame
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 : 我想要一个看起来像这样的输出,这意味着过滤器df1,以便仅保留df2等于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.
你能帮我吗。
这应该工作
df1[df2.State.astype(bool)]
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.