【问题标题】:How can I compare the excel and csv column using python如何使用 python 比较 excel 和 csv 列
【发布时间】:2020-05-11 20:21:53
【问题描述】:

我只想将 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

【问题讨论】:

  • 我正在使用这两种方法读取文件,但不知道如何应用条件进行比较。这个很模糊,你能更具体的问题是什么?请参阅How to Askhelp center

标签: python excel pandas csv compare


【解决方案1】:

试试这个:

 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 文件中有两个共同的行。

如果它对你有用,请告诉我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-13
    • 2023-04-09
    • 2016-06-08
    • 2016-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多