Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
900 views
in Technique[技术] by (71.8m points)

pandas - Update Row of One DF (df1) based off matching columns between 2 dataframes (df1, df2)

I have two dataframes

DF1:

EmployeeID CollectiveBargainingUnit BusinessGroup ProfitCenter Eligible
1          12A                      A12           UGZ.         
2          17A                      B12           MGZ.         
3          18A                      C12           DGZ.         
4          19A                      D12           XGZ.         

DF2:

CollectiveBargainingUnit BusinessGroup ProfitCenter Eligible
12A                      A12           UGZ.         True
17A                      B12           MGZ.         False
18A                      C12           DGZ.         False
19A                      D12           XGZ.         True
12A                      A13           UGZ.         False
27A                      C12           MKZ.         True
32A                      C22           DGZ.         True
19A                      D99           XGZ.         False

What would I would like to accomplish is this

EmployeeID CollectiveBargainingUnit BusinessGroup ProfitCenter Eligible
1          12A                      A12           UGZ.         True
2          17A                      B12           MGZ.         False
3          18A                      C12           DGZ.         False
4          19A                      D12           XGZ.         True

Based off of multiple columns between the two Dataframes (CollectiveBargainingUnit BusinessGroup ProfitCenter) I'd like to access it's eligibility in DF2 and update it in DF1

Where I am left off is:

eligibility_map=df2.groupby(["BusinessGroup","ProfitCenter","CollectiveBargainingUnit"]).first()["Eligible"]

df1["Eligible"] = df1["BargainingUnit"].map(eligibility_map).fillna(df1["Eligible"])

I can't map it to only one column. I need to map to multiple columns at once.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can merge df1 with df2 based on the required columns. Also, you have to drop Eligible in df1 before merging as it is not required.

(df1.drop('Eligible', axis=1)
 .merge(df2, on=['CollectiveBargainingUnit', 'BusinessGroup','ProfitCenter'], how='inner'))

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.5k users

...