0

I have 2 dataframes:

Reference_DF:

root_cause_level3   root_cause_level2   root_cause_level1
NA                  NA                  NA
Natural Disaster    Natural Disaster    Natural Disaster
Other               Other               Other
Communications      Misunderstood       Long Message

Actual_DF:

incident_id        root_cause_level3       root_cause_level2   root_cause_level1
XXXXXXXX-1         Communications       
XXXXXXXX-2         NA       
XXXXXXXX-3         NA       
XXXXXXXX-4         Other        
XXXXXXXX-1         Natural Disaster 

My goal is to map from Reference DF to Actual DF the level2 and level1 root causes. I want to map it based on matching level3 root causes. I don't know how to do that, any suggestions on how to solve this problem?

Expected output:

incident_id        root_cause_level3       root_cause_level2   root_cause_level1
XXXXXXXX-1         Communications          Misunderstood       Long Message
XXXXXXXX-2         NA                      NA                  NA
XXXXXXXX-3         NA                      NA                  NA       
XXXXXXXX-4         Other                   Other               Other        
XXXXXXXX-1         Natural Disaster        Natural Disaster    Natural Disaster
Sam
  • 641
  • 1
  • 7
  • 17

1 Answers1

0

You should be able to use .merge() like this:

new_df = pandas.merge(actual_df, reference_df, on='root_cause_level3', how='left')

a cleaner way could be to do something like:

new_df = pandas.merge(actual_df[['incident_id','root_cause_level3']], reference_df, on='root_cause_level3', how='left')

then you don't get duplicated column names in the new_df

wpercy
  • 9,636
  • 4
  • 33
  • 45