简体   繁体   中英

Map Values from one dataframe to another based two criteria

I have one dataframe (df) that contains home field adavantage values for various conferences. The first columns represents the home teams and the remaining column headers represent the away teams. I would like to create a new column, df1['hfa'] in the second dataframe (df1) that shows the home field advantage located in df for the respective matchups listed in df1. Below are images of the two dataframes. In excel I can achieve this with a two way lookup via Index Match. I would like to map the values from df to df1 for df1['hfa'] so that that they read: 2.5, 3.5, 4.5, etc. Thank you in advance!

I have the following code:

df1['hfa'] = df1['Home Team'].map(df.set_index('Home Team')[])

I am unsure what to use in the final argument to match the Away Team from df1 to the same column header.

df

df1

Based on what I undesrstand, this is what I have. See if this is what you looking for. Given Data:

在此处输入图片说明

(
    df.melt(id_vars=['Home Team'],
            value_vars=['North','East','South','West'],
            var_name='Away Team',
            value_name='hfa')
)

在此处输入图片说明

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