簡體   English   中英

如何使用 python 比較 excel 和 csv 列

[英]How can I compare the excel and csv column using 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)) 

我正在使用這兩種方法來讀取文件,但不知道如何應用條件進行比較。 這是文件: 在此處輸入鏈接描述

嘗試這個:

 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 文件中有兩個公共行。

請讓我知道它是否對您有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM