0

I have two dataframes :

df1 = pd.DataFrame({'Merge_Pr': ['BKK_AOT', 'BKK_BFS', 'BKK_TG', 'HND_ANA'],
                    'UniqueName': ['PR1', 'PR2', 'PR3', 'PR18']})

df2 = pd.DataFrame(
        {'Merge_Pr': ['BKK_AOT','BKK_AOT','BKK_BFS','BKK_TG','BKK_TG','HND_ANA','HND_ANA'],
         'Quantity':[9240, 1433, 56779, 2230, 5560, 1004, 4553],
         'Requisition_Number': ['NaN','NaN','NaN','NaN','NaN','NaN','NaN',]})

I would like to update the column Requisition_Number (df2) with PR number from the column UniqueName in (df1) by key column Merge_Pr.

I tried with merge but it adds a new column to df2 but what I want is to have the result in the existing column Requisition_Number so the desired output would be:

    Merge_Pr    Quantity    Requisition_Number

0   BKK_AOT      9240         PR1
1   BKK_AOT      1433         PR1
2   BKK_BFS      56779        PR2
3   BKK_TG       2230         PR3
4   BKK_TG       5560         PR3
5   HND_ANA      1004         PR18
6   HND_ANA      4553         PR18

Thanks a lot for any suggestions

1 Answer 1

1

One solution: map Merge_Pr to a dictionary made from the two columns in df1.

df2.Requisition_Number = df2.Merge_Pr.map(dict(zip(df1.Merge_Pr, df1.UniqueName)))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.