简体   繁体   English

我有两个数据框(DF1)和(DF2)。 我想替换 (DF2) 中与 (DF1) 的两列上的条件匹配的列的值

[英]I have two dataframes (DF1) and (DF2). I want to substitute values for a column in (DF2) that match criteria on two columns of (DF1)

I want to populate the 'Salary' column in DataFrame1 (DF1) with the corresponding 'Salary' in DataFrame2 (DF2).我想用 DataFrame2 (DF2) 中的相应“Salary”填充 DataFrame1 (DF1) 中的“Salary”列。 These need to match on 'Team' AND 'Players'.这些需要匹配“团队”和“球员”。

To note:要注意:

The Dataframes are: Not the same size.数据框是: 大小不同。 Not the same order.不是同一个顺序。

import pandas as pd


#df 1:

nba_data = {'Team': ['Mavericks', 'Mavericks', 'Mavericks', '', 'NewYorkKnicks17','Houston Rockets', 'NewYorkKnicks17'], 
            'Players': ['Luka Doncic', 'Kristaps Porzingis', 'Jalen Brunson', 'Kristaps Porzingis', 'JR Smith',
                        'James Harden', 'Derrick Rose',],
            'Salary': ['0', '0', '0','0', '0', '0', '0'],
           'Coach': ['Rick Carlisle', 'Rick Carlisle', 'Steve Kerr', 'Phil Jackson', 'Tom Thibideou', '', '']}

nba_df1 = pd.DataFrame(nba_data)

nba_df1


#df2:

nba_data2 = {'Team': ['Mavericks', 'Mavericks', 'Mavericks', 'NewYorkKnicks17', 'NewYorkKnicks17', 'NewYorkKnicks17', 'Houston Rockets'], 
            'Players': ['Luka Doncic', 'Kristaps Porzingis', 'Steph Curry', 'JR Smith', 'Derrick Rose',
                        'Kristaps Porzingis', 'James Harden'],
            'Salary': ['3m', '126m', '0','115m', '0', '20m', '1.5m'],
            'Coach': ['Rick Carlisle', 'Rick Carlisle', 'Steve Kerr', '', 'Tom Thibideou', 'Phil Jackson', '']}


nba_df2 = pd.DataFrame(nba_data2)

nba_df2

Result desired = nba_df1 with the appropriate salaries populated (run the below):所需的结果 = nba_df1 并填充了适当的薪水(运行以下命令):

nba_data3 = {'Team': ['Mavericks', 'Mavericks', 'Mavericks', '', 'NewYorkKnicks17','Houston Rockets', 'NewYorkKnicks17'], 
            'Players': ['Luka Doncic', 'Kristaps Porzingis', 'Jalen Brunson', 'Kristaps Porzingis', 'JR Smith',
                        'James Harden', 'Derrick Rose',],
            'Salary': ['3m', '126m', '0','20m', '115m', '1.5m', '0'],
           'Coach': ['Rick Carlisle', 'Rick Carlisle', 'Steve Kerr', 'Phil Jackson', 'Tom Thibideou', '', '']}



nba_df1_adjusted = pd.DataFrame(nba_data3)






Kindly note: this is not a tutorial. - it is a specific question and therefore not a duplicate of a general tutorial. 
agg = pd.merge(nba_df1, nba_df2, on = ['Players', 'Team'], how = 'left')

Your result will be on Salary_y您的结果将在 Salary_y 上

Edit: Kind of dirty but it works:编辑:有点脏,但它有效:

agg = pd.merge(nba_df1, nba_df2[['Team', 'Players', 'Salary']], on = ['Players', 'Team'], how = 'left')
agg2 = pd.merge(nba_df1, nba_df2, on = ['Players', 'Coach'], how = 'left')

merge = pd.merge(agg, agg2, on = ['Players', 'Coach'])

merge['Salary'] = merge['Salary_y_x'].fillna(merge['Salary_y_y'])

暂无
暂无

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

相关问题 我有两个数据帧 DF1 和 DF2,在特定索引处满足从 DF2 到 DF1 的条件的 append 行的最佳方法是什么? - I have two dataframes DF1 and DF2, what is the best way to append rows that meet a conditional from DF2 to DF1 at specific indices? 合并 df1 列中值的两个数据框,以 df2 列中的逗号分隔值和 df1.Column2 = df2.Column2 - Merge two dataframes on value in column of df1 in comma separated values in column of df2 AND df1.Column2 = df2.Column2 在 df2 列中以逗号分隔值合并 df1 列中的值的两个数据框 - Merge two dataframes on value in column of df1 in comma separated values in column of df2 如果两个不同数据帧中两列的值匹配,则将df2中另一列的值复制到df1中的列 - If values from two columns in two different data frames match then copy values from another column in df2 to column in df1 一列 (df1) 与两列 (df2) 与 python 进行比较 - One column (df1) compare with two column(df2) with python pandas 如何从 df2 获取 df1 的值,而 df1 和 df2 的值在列上重叠 - pandas how to get values from df2 for df1 while df1 and df2 have values overlapped on column(s) 我有两个数据框 df1 和 df2,我需要使用 df2 中的键过滤掉 df1,使用 df2 中的开始和结束日期,我需要得到像 df3 这样的结果 - I have two data frames df1 and df2, I need to filter out df1 using keys in df2 using start and end dates in df2, I need to get a result like df3 Pandas Dataframes-根据df1中的条件填充df2 - Pandas Dataframes - Populating df2 based on criteria in df1 我可以使用df1中的一列和df2中的单元格中的任何值之一来连接两个数据帧吗? - Can I join two data frames using one column in df1 and one of any values in a cell in df2? 根据 df2 中的条件查找 df1 中的列值 - Looking up column value in df1 based on criteria in df2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM