Search code examples
pythonpandasdataframematchinsert-update

Look up values when the columns names of two dataframes are a match


I would like to write a function that updates the values of df1 when the column names of df1 and df2 match each other.

For example: df1:

    Name | Graduated | Employed | Married
    AAA       1           2         3
    BBB       0           1         2 
    CCC       1           0         1

df2:

    Answer_Code | Graduated | Employed | Married
       0                No         No        No
       1                Yes       Intern    Engaged
       2                N/A        PT        Yes
       3                N/A        FT      Divorced 

Final Result: df3:

     Name | Graduated |   Employed   |  Married
     AAA       Yes          PT         Divorced
     BBB       No           Intern     Yes 
     CCC       Yes          No         NO

I would like to code something like this:

     IF d1.columns = d2.columns THEN 

     df1.column.update(df1.column.map(df2.set_index('Answer_Code').column))

Solution

  • One method is to utilise pd.DataFrame.lookup:

    df1 = pd.DataFrame({'Name': ['AAA', 'BBB', 'CCC'],
                        'Graduated': [1, 0, 1],
                        'Employed': [2, 1, 0],
                        'Married': [3, 2, 1]})
    
    df2 = pd.DataFrame({'Answer_Code': [0, 1, 2, 3],
                        'Graduated': ['No', 'Yes', np.nan, np.nan],
                        'Employed': ['No', 'Intern', 'PT', 'FT'],
                        'Married': ['No', 'Engaged', 'Yes', 'Divorced']})
    
    # perform lookup on df2 using row & column labels from df1
    arr = df2.set_index('Answer_Code')\
             .lookup(df1.iloc[:, 1:].values.flatten(),
                     df1.columns[1:].tolist()*3)\
             .reshape(3, -1)
    
    # copy df1 and allocate values from arr
    df3 = df1.copy()
    df3.iloc[:, 1:] = arr
    
    print(df3)
    
      Name Graduated Employed    Married
    0  AAA       Yes       PT   Divorced
    1  BBB        No   Intern        Yes
    2  CCC       Yes       No    Engaged