简体   繁体   English

如何使用 python 比较 excel 和 csv 列

[英]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.我只想将 wedartmore.csv 的 A 列与 Book1.xlsx 的 C 列进行比较,并希望获得值相同的索引。

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. CSV 和 xlsx 文件中有两个公共行。

Please let me know if it worked for you.请让我知道它是否对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用python比较两个不同的csv文件? - How can I compare two different csv file using python? 如何比较python中csv文件的列中的值? - How do I compare values within a column in a csv file in python? 如何让python将抓取的数据与填充的csv进行比较? - How can I get python to compare scraped data to a populated csv? 使用 python 将 2 个 excel 文件与参考列进行比较 - Compare 2 excel file with a reference column using python 如何使用 openpyxl 将一个 excel 文件的列值与 Python 中另一个 excel 文件的列值进行比较? - How to compare column values of one excel file to the column values of another excel file in Python using openpyxl? 如何使用Python比较日期? - How can I compare dates using Python? 如何按列比较两个CSV文件并使用Pandas Python将CSV文件中的差异保存 - How to compare two CSV files by column and save the differences in csv file using pandas python 如何比较python中两个excel之间的列? - how to compare column between two excel in python? 如何使用Python比较Excel中的相邻单元格? - How do I compare adjacent cells in excel using Python? 使用python矩阵M(使用numpy),如何将该矩阵的每一列与所有其他列进行比较? - With a python matrix M (using numpy), how can I compare each column of that matrix with all other columns?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM