Home > Mobile >  How to use Vlookup in csv with python?
How to use Vlookup in csv with python?

Time:05-29

Original file

enter image description here

Data source

enter image description here

Output

enter image description here

My code is as follows.

import pandas as pd

file_dest = r"C:\Users\user\Desktop\Book1.csv"

# read csv data
book=pd.read_csv(file_dest)
file_source = r"C:\Users\user\Desktop\Book2.csv"

materials=pd.read_csv(file_source)

Right_join = pd.merge(book,
                      materials,
                      on ='Name',
                      how ='left')

Right_join.to_csv(file_dest, index=False)

However, the output is as follows, which looks like it just copied the contents but didn't use Vlookup to insert the data. I had tried it with different kinds of data. The results are all the same (which looks like it just copied the contents). Please help me find out the bugs.

enter image description here

CodePudding user response:

Since column names are different in each data source, you have to specify columns to join on in the left and right dataframes. Try this:

# assuming materials is your data source with Price column
joined = book.merge(materials,
                  left_on="Custmor",
                  right_on="Name",
                  how ='left')
  • Related