# 基于两列合并两个数据框

##### 问题描述投票：0回答：2

``````   phone_number1  phone_number2
1    123             123
2    345              0
3    678             678
4    912             912
5    555             666
``````

`df_out`

``````    phone_number  address   name
1      123         add1     name1
2     777777       add2     name2
3      666         add3     name3
4      555         add4     name4

``````

``````    phone_number  address   name    df1_phone_number1    df1_phone_number2
1      123         add1     name1        123                  123
2     777777       add2     name2        None                 None
3      666         add3     name3        None                 666
4      555         add4     name4        555                  None

``````

``````    mergedStuff3 = pd.concat([
pd.merge(df1, df_out,  how='right', left_on=['phone_number1'], right_on = ['phone_number']),
pd.merge(df1, df_out,  how='right', left_on=['phone_number2', 'phone_number1'], right_on = ['phone_number', 'phone_number'])

])
``````
python python-3.x pandas dataframe
##### 2个回答
1

``Series.isin``

``````m1 = df_out['phone_number'].isin(df1['phone_number1'])
m2 = df_out['phone_number'].isin(df1['phone_number2'])
df_out['df1_phone_number1'] = df_out['phone_number'].where(m1)
df_out['df1_phone_number2'] = df_out['phone_number'].where(m2)
print (df_out)
phone_number address   name  df1_phone_number1  df1_phone_number2
1           123    add1  name1              123.0              123.0
2        777777    add2  name2                NaN                NaN
3           666    add3  name3                NaN              666.0
4           555    add4  name4              555.0                NaN
``````

0

``numpy.where``

``numpy.where``