Update dataframe based on match in another dataframe if condition is met

Viewed 5086

I have two dataframes and I want to update the first one. df1 contains different Markets (M1, M2, etc.) and some Codes for each of them (numbers or Dummy)

import pandas as pd
labels = ["Market","Code"]
values = [["M1","1234"],["M1","Dummy"],["M1","1234"],["M2","Dummy"],["M1","1234"]]
df = pd.DataFrame.from_records(values,columns=labels)
print(df)
 Market   Code
0     M1   1234
1     M1  Dummy
2     M1   1234
3     M2  Dummy
4     M1   1234

If Code == Dummy, then I want to update the Code in df based on the value in df2 for this specific market. So each Market should receive a different new Code.

labels = ["Market","Code(New)"]
values = [["M1","4567"],["M2","5678"]]
df2 = pd.DataFrame.from_records(values,columns=labels)
print(df2)
 Market Code(New)
0     M1      4567
1     M2      5678

in the end I should get

labels = ["Market","Code"]
values = [["M1","1234"],["M1","4567"],["M1","1234"],["M2","5678"],["M1","1234"]]
df_clean = pd.DataFrame.from_records(values,columns=labels)
print(df_clean)
  Market  Code
0     M1  1234
1     M1  4567
2     M1  1234
3     M2  5678
4     M1  1234
2 Answers
Related