基于两列合并两个数据框

问题描述 投票:0回答:2

我有两个数据帧:df1df_outdf1看起来像这样

   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

我尝试过此代码,但不会保存与phone_number2相匹配的数据

    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.where支票隶属关系的Series.where

Series.isin

或使用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
推荐问答
热门问答
最新问答