根据两个条件从熊猫数据框中提取值

时间:2019-02-11 10:22:28

标签: python pandas dataframe

我有两个数据帧,df1df2

df1 = A B
      1 a
      1 
      1 5
      1 b
      1 c
      1 d


df2 = A B C
      1 a apple
      1  cherry
      1 5 apple
      1 b orange

我想基于A和B列合并这两个数据框。我的逻辑如下:

if df1['A'][0] is in df2['A'] and df1['B'][0] is in df2['B'] and they are equal:

      then create new column df1['New Product'] = df2['C'] 

如果满足此条件,我需要在df1中创建第三列。

我很努力,但没有成功。我想索引位置很重要。

这是我无法解决的解决方案:

df1['New Product'] = df2['C'][(df1['A'].isin(df2['A'])) & (df1['B'].isin(df2['B']))] 

预期输出应为:

df1 = A B C
      1 a apple
      1  cherry
      1 5 apple
      1 b orange
      1 c nan
      1 d nan 

2 个答案:

答案 0 :(得分:1)

尝试简单的左联接,

df=pd.merge(df1,df2,on=['A','B'],how='left').rename(columns={'C':'New Product'})

O / P:

   A  B New Product
0  1  a       apple
1  1         cherry
2  1  5       apple
3  1  b      orange
4  1  c            
5  1  d            

答案 1 :(得分:1)

您需要:

import pandas as pd

df1 = pd.DataFrame({'A':[1]*6, 'B':['a',None,5,'b','c','d']})
df2 = pd.DataFrame({'A':[1]*4, 'B':['a', None, 5, 'b'], 'C':['apple','cherry','apple','orange']})

df = df1.merge(df2, how='left', on=['A','B'])
print(df)

输出:

 A     B       C                                                                                                                    
0  1     a   apple                                                                                                                    
1  1  None  cherry                                                                                                                    
2  1     5   apple                                                                                                                    
3  1     b  orange                                                                                                                    
4  1     c     NaN                                                                                                                    
5  1     d     NaN