简体   繁体   中英

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))

You can use map .

Example:

df1.Graduated.map(df2.Graduated)

yields

0    Yes
1     No
2    Yes

Thus just do that for every column, as follows

for col in df1.columns:
    if col in df2.columns:
        df1[col] = df1[col].map(df2[col])

Remember to set the index to the answer code first, ie df2 = df2.set_index("Answer_Code") , if necessary.

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

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