简体   繁体   中英

How can I compare the excel and csv column using python

I just want to compare column A from wedartmore.csv to column C of Book1.xlsx and want to get index where values are same.

import csv 

# opening the CSV file 
with open('wedartmore.csv', mode ='r')as file: 

# reading the CSV file 
csvFile = csv.reader(file) 

# displaying the contents of the CSV file 
for lines in csvFile: 
        print(lines[0]) 

# Reading excel file:

import xlrd 

loc = ("Book1") 

wb = xlrd.open_workbook(loc) 
sheet = wb.sheet_by_index(0) 
sheet.cell_value(0, 0) 

for i in range(sheet.nrows): 
    print(sheet.cell_value(i, 0)) 

I am using these two methods for reading files but don't know how to apply condition for comparing. Here are the files: enter link description here

Try this:

 import pandas as pd

 df_csv = pd.read_csv('wedartmore.csv')
 df_xlsx = pd.read_excel('Book1.xlsx')
 merged_data = df_csv.merge(df_xlsx, left_on = 'Name', right_on = 'Artist')

 print(merged_data)

There are two common rows in the CSV and xlsx files.

Please let me know if it worked for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM