简体   繁体   English

如何使用Python比较Excel中的相邻单元格?

[英]How do I compare adjacent cells in excel using Python?

I have two columns and I am trying to return the total count of rows where both adjacent cells within the two columns are identical. 我有两列,我试图返回两列中相邻单元格相同的行总数。 I am trying to iterate through each row of two columns and compare each item in the first column with its adjacent item in the second column ie 我试图遍历两列的每一行,并将第一列中的每个项目与第二列中的相邻项目进行比较,即

A | B
1 | 1
2 | 3
4 | 4

returns 2 for there are 2 pairs that are identical. 返回2,因为有两对相同。

My two columns are Q and R, so far: 到目前为止,我的两列是Q和R:

import openpyxl

excel_document = openpyxl.load_workbook('example.xlsx')


sheet = excel_document.get_sheet_by_name('Page 1')
created_closed = sheet['Q2':'R1844']
count = 0

for cell in column:
    if Q[2] == R[2]: #something along the lines of this
          count += 1
import pandas as pd

df = pd.read_excel('example.xlsx')
df = df[df['A'] == df['B']]
print (df.shape[0])

The answer that comes to mind is: 想到的答案是:

count = created_closed[created_closed['Q']==created_closed['R']].shape[0]

No for loop required because pandas takes care of that. 没有for循环必需的,因为熊猫需要的照顾。

A - Try using numpy and pandas . A - 尝试使用numpypandas

1- Find all intersections: This will return an array with the elements that were common in both columns 1-查找所有交叉点:这将返回一个数组,其中包含两列中常见的元素

from pandas import read_excel
import numpy as np

df = read_excel('excel_data.xlsx', names=['A','B'], header=None)
np.intersect1d(df['A'], df['B'])

2- now count the length of the array 2-现在计算数组的长度

B- read each column and save to, two separate dictionary with key being position and value being the value at that position . B-读取每一column并保存到两个单独的dictionary其中keypositionvalue是该positionvalue Compare the two dictionaries and keep track of matches. 比较两个dictionaries并跟踪比赛。

something like this, I am traveling right now so I can no test. 像这样的东西,我现在正在旅行,所以我无法测试。

count = 0
col1 = {1:'a', 2:'b', 3:'c'}
col2= {1:'g', 2:'b', 3:'v'}

for key in col1.keys():
    if col1[key] == col2[key]:
        count = count + 1

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

相关问题 如何在 Python 中使用 xlwings select excel 的非相邻范围或单元格? - How to select non-adjacent ranges or cells of excel using xlwings in Python? 使用xlwt python比较2个单元格并在excel中更新另一个单元格中的结果 - compare 2 cells and update result in another cell in excel using xlwt python 如何使用 Python 比较 Excel 中的多个列? - How do you compare multiple columns in Excel using Python? 如何使用 python 比较 excel 和 csv 列 - How can I compare the excel and csv column using python 如何使用 Python 将 Excel 中的空白单元格替换为 0? - How to replace the blank cells in Excel with 0 using Python? 如果相邻单元格是 python 中的连续空白,如何合并到列的单元格 - How to merge to cells of a column if the adjacent cells are consecutive blanks in python 如何使用 Python 删除列中单元格的某些部分? - How do I remove certain parts of cells in a column using Python? 如何在python zipfile中引用相邻的库 - how do I reference an adjacent library in a python zipfile 如何在python中将相邻和相同颜色的像素组合在一起? - How do I group adjacent and equal colored pixel together in python? 如何在 n 维网格中找到一个点的所有相邻单元格? - How do I find all adjacent cells of a point in an n-dimensional grid?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM