I'm trying to use Vlookup in Python from two excel files. One file is an original file and the other one is run through some logics. In the original file, column 3 is empty and the other file is filled with data in column 3.
Here is the original file:
#Original file
col1 | col2 | col3
a b
c d
e f
g h
i j
k l
m n
...
Here is the second file:
#Second file
col1 | col2 | col3
a b 1
c d 2
e f 3
g h 4
i j 3
k l 1
m n 2
...
What I want to do is to merge these files into one by using 'VLOOKUP' function. Here is my code:
import pandas as pd
import openpyxl
df1 = pd.read_excel("original.xlsx")
df2 = pd.read_excel("second.xlsx")
#Merge
result = pd.merge(df1, df2, on = ['col1'], how='left')
result_columns = result.columns.str.replace('_x|_y','', regex=True)
result.to_excel("result.xlsx", index=False)
However, when I run through the above code, the file with results shows only the merged data and not the filled data in column 3. Also, there are duplicated columns and data generated with few blanks at the end of original columns in the result file.
Here is my result file:
col1 | col2 | col3 | col2 | col3
a b b 1
c d d 2
e f f
g h h 4
i j j
k l l 1
m n n 2
...
My desired output is if second file col1 would match with col1 in the original file, fill the 'col3' in original file
For example:
col1 | col2 | col3
a b 1
c d 2
e f 3
g h 4
i j 3
k l 1
m n 2
...
Anyone has any idea?