dataframe1 =
id number count1 count2
1 5225 10 30
2 2222 3 40
3 7121 30 50
dataframe2 =
id value1 value2
1 4000 6000
3 2500 3300
3 7000 8000
输出
id value1 value2 number count1 count2
1 4000 6000 5225 10 30
3 7000 8000 7121 30 50
我试过了
dataframe2['a']=(dataframe1['number']>= dataframe2['value1']) & (dataframe1['number'] <= dataframe2['value2'])
它在a列中给出了True False值,我要做的是:对于Dataframe1“数字”列中的每个值,搜索它是否等于Dataframe2的“ value1”和“ value2”对值中的任何一个。此外,对于此“ number1”和“ number2”对值,其相应的“ id”必须与数据帧中的“ id”匹配。如果这都是真的,那么我想在dataframe2中插入数字,count1和count2
merged = df1.join(df2, on="id", how="inner")
result = merged.query("value1 <= number <= value2")
result
number count1 count2 value1 value2
id
1 5225 10 30 4000 6000
3 7121 30 50 7000 8000
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句