简体   繁体   中英

Pandas merge multiple columns with conditions

I have to perform multiple mergers and I am looking for a better way than writing each time the same code, creating 4 dataframes, concat them and merge them again with the original one.

I have 2 dataframes both with 2 columns containing numbers. I would like to match this 4 columns and output the matched number.

This is the example:

df1 = pd.DataFrame({'Name':['John','Michael', 'Sam'], 'Tel1':['2222','3333', '1111'], 'Tel2':[np.nan, np.nan, '5555']})

df2 = pd.DataFrame({'Second Name':['Smith','Cohen','Moore','Kas', 'Faber'], 'Tel3':['888','3333',np.nan , np.nan, np.nan], 'Tel4':[np.nan, np.nan, np.nan , '1111', np.nan]})

Expected output: 在此处输入图片说明

My Code:

df1_temp = pd.merge(df1,df2, left_on='Tel1', right_on='Tel3', how='left')
df2_temp = pd.merge(df1,df2, left_on='Tel1', right_on='Tel4', how='left')
df3_temp = pd.merge(df1,df2, left_on='Tel2', right_on='Tel3', how='left')
df4_temp = pd.merge(df1,df2, left_on='Tel2', right_on='Tel4', how='left')

concat = pd.concat(df1_temp...)

You can melt the data then merge:

df1['Second Name'] = (df1[['Tel1','Tel2']]
    .reset_index()
    .melt('index')
    .dropna()
    .merge(df2.melt('Second Name').dropna(),on='value')
    .set_index('index')['Second Name']
)

Output:

      Name  Tel1  Tel2 Second Name
0     John  2222   NaN         NaN
1  Michael  3333   NaN       Cohen
2      Sam  1111  5555         Kas

This is not a whole lot shorter but it does remove one step.

concat = pd.concat([df1.merge(df2,left_on='Tel1', right_on='Tel3',how='left'), 
                    df1.merge(df2,left_on='Tel1', right_on='Tel4',how='left'),
                    df1.merge(df2,left_on='Tel2', right_on='Tel3',how='left'),
                    df1.merge(df2,left_on='Tel2', right_on='Tel4',how='left')])

# Drop duplicates
concat.drop_duplicates(inplace=True)



 Name   Tel1    Tel2    Second Name Tel3    Tel4
0   John    2222    NaN             NaN  NaN    NaN
1   Michael 3333    NaN           Cohen 3333    NaN
2   Sam     1111    5555            NaN NaN     NaN
1   Michael 3333    NaN             NaN NaN     NaN
2   Sam     1111    5555            Kas NaN     1111
0   John    2222    NaN           Moore NaN     NaN
1   John    2222    NaN             Kas NaN     1111
2   John    2222    NaN           Faber NaN     NaN
3   Michael 3333    NaN           Moore NaN     NaN
4   Michael 3333    NaN             Kas NaN     1111
5   Michael 3333    NaN           Faber NaN     NaN
0   John    2222    NaN           Smith 888     NaN
1   John    2222    NaN           Cohen 3333    NaN
4   Michael 3333    NaN           Smith 888     NaN

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM