Search code examples
dataframedictionaryjoinassign

Assign column values from another dataframe with repeating key values


Please help me in Pandas, i cant find good solution Tried map, assign, merge, join, set_index. Maybe just i am too tired :)

df:

   m_num  A  B
0      1  0  9
1      1  1  8
2      2  2  7
3      2  3  6
4      3  4  5
5      3  5  4

df1:

   m_num   C
0      2  99
1      2  88

df_final:

   m_num  A  B  C
0      1  0  9  NaN
1      1  1  8  NaN
2      2  2  7  99
3      2  3  6  88
4      3  4  5  NaN
5      3  5  4  NaN

Solution

  • Try:

    df2 = df[df['m_num'].isin(df1['m_num'])].reset_index(drop=True)
    df2 = pd.merge(df2,df1,on=[df1.index,'m_num']).drop('key_0',axis=1)
    df2 = pd.merge(df,df2,on=['m_num','A','B'],how='left')
    print(df2)
    

    Prints:

       m_num  A  B     C
    0      1  0  9   NaN
    1      1  1  8   NaN
    2      2  2  7  99.0
    3      2  3  6  88.0
    4      3  4  5   NaN
    5      3  5  4   NaN
    

    Explanation:

    There may be better solutions out there but this was my thought process. The problem is slightly tricky in the sense that because 'm_num' is the only common key and it and it has repeating values.

    So first I created a dataframe matching df and df1 here so that I can use the index as another key for the subsequent merge.

    df2 = df[df['m_num'].isin(df1['m_num'])].reset_index(drop=True)
    

    This prints:

       m_num  A  B
    0      2  2  7
    1      2  3  6
    

    As you can see above, now we have the index 0 and 1 in addition to the m_num as key which we can use to match with df1.

    df2 = pd.merge(df2,df1,on=[df1.index,'m_num']).drop('key_0',axis=1)
    

    This prints:

       m_num  A  B   C
    0      2  2  7  99
    1      2  3  6  88
    

    Then tie the above resultant dataframe to the original df and do a left join to get the output.

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