在多个条件下匹配2个df

时间:2019-04-02 06:34:44

标签: python pandas match

我正在尝试根据2个条件从df2获取相应的df1值。 这是2个dfs。 df1:

    name value1
0   abc  3      
1   cde  1      
2   cde  4      
3   ijk  6      

df2:

    name value1 value2
0   abc  3      5
1   cde  3      5
2   cde  1      1
3   ijk  2      3

我想在df1中获取名称匹配且已验证df1.value1.between(df2.value1, df2.value2)的df2.value2

我尝试过:

df1['value2'] = df2[(df2['name']==df1['name'])(df1['value1'].between(df2['value1']., df2['value2']))]['value2']

但没有运气..你们有什么建议吗?

在这里您可以看到我在df1上的预期输出:

    name value1 value2
0   abc  3      5
1   cde  1      1
2   cde  4      5
3   ijk  6      nan

2 个答案:

答案 0 :(得分:1)

您需要merge两列在一起,然后按between进行过滤,如果必要,缺少不匹配值的缺失值,请添加merge并使用左连接:

print (df1)
  name  value1  col
0  abc       3    1
1  cde       1    5
2  cde       4    7
3  ijk       6    3


df = df1.merge(df2, on='name', how='outer', suffixes=('','_'))

df = df[df.value1.between(df.value1_, df.value2)]
df = df1.merge(df[df2.columns], how='left')
print (df)
  name  value1  col  value2
0  abc       3   10     5.0
1  cde       1   50     1.0
2  cde       4   70     5.0
3  ijk       6   30     NaN

答案 1 :(得分:0)

您还可以使用numpy.where()

df1['value2'] =np.where(df1.value1.between(df2.value1, df2.value2), df2['value2'], np.nan)