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
我希望获得一个数据框,其中包含df1中以下条件的行:
- df1中列Points的值也在df2的列Points中
- df2中列Sum的值介于0和2之间
Points ColX
4 5 5
我试过以下的方法但它没有起作用:
df1[df1.merge(df2, on = 'Points')['Sum'] <= 2 and ['Sum']>=0]
你能帮我找到正确的代码吗?