简体   繁体   中英

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

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

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