python - 如何使用 python 比较 excel 和 csv 列
问题描述
我只想将 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 文件中有两个常见的行。
请让我知道它是否对您有用。
推荐阅读
- javascript - 使用属性值从对象数组中查找并返回对象
- javascript - 如何访问 console.log 中显示的对象属性
- python-3.x - 使用具有相同 id 但避免某些值的非缺失值填充缺失的 pandas 数据
- python - 如何从另一个python文件调用def python
- javascript - 使用 d3 或 cytoscape 渲染家谱
- javascript - 通过 app.js 连接到本地 MongoDB 服务器时遇到问题
- amazon-iam - 谁创建用户 default_user?
- python - 图像识别模型每次都猜测同一个类
- algorithm - 稳定的婚姻问题 - 空的偏好列表
- html - Angular - 我需要为我的选项分配一个默认值