I have written a code that merges File B into File A based on a column ‘Code’. Some of the values from File B, however, are generic (‘Color’) and I would need to do another merge with file C – but instead of creating a new column I would like to use the same column created during the first merge and whenever first merge returned value ‘Color’ only for those rows do merge with file C to get the proper value.
I went as far as merging A with B:
JavaScript
x
9
1
import pandas as pd
2
3
File_A = pd.read_excel(r'.../My Files/Python/Supplier cat testing/File A.xlsx')
4
File_B = pd.read_excel(r'.../My Files/Python/Supplier cat testing/File B.xlsx')
5
File_C = pd.read_excel(r'.../My Files/Python/Supplier cat testing/File C.xlsx')
6
results = pd.merge(File_A, File_B[['Code','Color']], on='Code')
7
8
results.to_excel('Output_File.xlsx', index=False)
9
Would anyone have any idea where do I even start, please?
Advertisement
Answer
Try :
JavaScript
1
4
1
dfOut = dfB.copy()
2
dfOut['Color'] = dfB.merge(dfC, on='Code').apply(lambda r: r.Color_x if r.Color_y == 'Color' else r.Color_y, axis=1)
3
print(df)
4
Output
JavaScript
1
7
1
Code Color
2
0 0 Green
3
1 1 Yellow
4
2 2 Orange
5
3 3 Red
6
4 4 Black
7