如何使用python比较excel和csv列

2024-03-29 08:08:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我只想比较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)) 

我使用这两种方法读取文件,但不知道如何应用条件进行比较。 以下是文件:enter link description here


Tags: csvthecsvfileinimportforopenfile
1条回答
网友
1楼 · 发布于 2024-03-29 08:08:56

试试这个:

 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)

CSV和xlsx文件中有两个常用行

请让我知道它是否适合你

相关问题 更多 >