[英]How to extract rows of a pandas dataframe according to conditions based on another dataframe
I have these two dataframes :我有这两个数据框:
df1 = pd.DataFrame({'Points':[1,2,3,4,5], 'ColX':[9,8,7,6,5]})
df1
Points ColX
0 1 9
1 2 8
2 3 7
3 4 6
4 5 5
df2 = pd.DataFrame({'Points':[2,5], 'Sum':[-1,1], 'ColY':[2,4]}) # ColY does not matter, I just added it to say that this dataframe can have other columns that the useful columns for this topic
df2
Points Sum ColY
0 2 -1 2
1 5 1 4
I would like to get a dataframe with the rows of df1 where :我想获得一个包含 df1 行的数据框,其中:
Consequently, I would like to get this dataframe (no matter the index) :因此,我想获得这个数据框(无论索引如何):
Points ColX
4 5 5
I tried the following but it didn't work :我尝试了以下但没有奏效:
df1[df1.merge(df2, on = 'Points')['Sum'] <= 2 and ['Sum']>=0]
Could you please help me to find the right code ?你能帮我找到正确的代码吗?
Try this:尝试这个:
df1[df1['Points'].isin(df2.query('0 <= Sum <= 2')['Points'])]
Output:输出:
Points ColX
4 5 5
Explained:解释:
df2.query('0 <= Sum <=2')
to filter df2 first to only valid records df2.query('0 <= Sum <=2')
将 df2 首先过滤为仅有效记录isin
of filter df2 Points column.isin
滤波器DF2点列。Use Series.between
for boolean mask with boolean indexing
for filtering passed to another mask with Series.isin
:使用
Series.between
布尔掩码与boolean indexing
过滤传递到另一个掩码与Series.isin
:
df = df1[df1['Points'].isin(df2.loc[df2['Sum'].between(0,2), 'Points'])]
print (df)
Points ColX
4 5 5
Your solution should be changed with DataFrame.query
for filtering:您的解决方案应该使用
DataFrame.query
进行更改以进行过滤:
df = df1.merge(df2, on = 'Points').query('0<=Sum<=2')[df1.columns]
print (df)
Points ColX
1 5 5
also works:也有效:
df3 = df1.merge(df2, on='Points')
result = df3[(df3.Sum >= 0) & (df3.Sum <= 2)]
result
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.