简体   繁体   English

基于两个标准将值从一个数据帧映射到另一个数据帧

[英]Map Values from one dataframe to another based two criteria

I have one dataframe (df) that contains home field adavantage values for various conferences.我有一个数据框 (df),其中包含各种会议的主场优势值。 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.我想在第二个数据框 (df1) 中创建一个新列 df1['hfa'],它显示了 df1 中列出的各个比赛的主场优势。 Below are images of the two dataframes.下面是两个数据框的图像。 In excel I can achieve this with a two way lookup via Index Match.在 excel 中,我可以通过索引匹配进行双向查找来实现这一点。 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!我想将 df1['hfa'] 的值从 df 映射到 df1,以便它们读取:2.5、3.5、4.5 等。在此先感谢您!

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.我不确定在最后一个参数中使用什么来匹配来自 df1 的 Away Team 到相同的列标题。

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM