0

I have a reference dataframe that contains a column of ID's and two columns with ID attributes (function and chapter). I want to cross reference two other data frames to see if: 1) the IDs from each of the two dataframes match the reference dataframe. 2) Of those matching IDs from the reference dataframe and the other two dataframes Im comparing, pull the corresponding data from the reference dataframe (i.e., function/chapter) and align to matched ID's.

I have tried writing a function a long with sorting the values into a list or dictionary and pull all those that align and those that don't align.

to match ID's

Updated_list = []
Mismatch_list = []
for key in res.keys():
    if key in Expanded_dic.keys():
        Updated_list.append(key)
    elif key not in Expanded_dic.keys():
        Mismatch_list.append(key)
print(Updated_list)

Reference DF: ID | Chapter | Function

Of the two dataframes with their own ID column, I want to output a new dataframe that shows matching ID's as well as it's corresponding Chapter and Function value (if matched).

Expected output: enter image description here

Dinho
  • 704
  • 4
  • 15
  • It sounds like you just want an inner join. Please refer to https://stackoverflow.com/questions/53645882/pandas-merging-101 – Ben Pap Jul 22 '19 at 22:22
  • Please take a look at [How to create good, reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) and provide a [mcve] including sample input and output in the text of your question, not as pictures – G. Anderson Jul 22 '19 at 22:23
  • @BenPap I looked at that link, although helpful it is not quite what I need. Bascially those columns in the output dataframe are empty. I'm pulling values from the referenced DF that have the same ID's as the other two DFs Im comparing too. Does that make sense? – Dinho Jul 22 '19 at 23:51
  • `1) the IDs from each of the two dataframes match the reference dataframe:` This is done by an inner join on the reference df and each of the 2 df's on their ID columns. You will get back the ID's that match (assuming your reference has all the ID's otherwise you will have to do a Left or Right join).`2) Of those matching IDs from the reference dataframe and the other two dataframes Im comparing, pull the corresponding data from the reference dataframe (i.e., function/chapter) and align to matched ID's`: This will automatically happen with the join. – Ben Pap Jul 23 '19 at 00:15

0 Answers0