遍历Excel中的列并比较单元格

时间:2018-08-13 01:49:21

标签: python excel openpyxl

我有两列,我想将每个单元格与其相邻的单元格进行比较,并在每次有副本时将变量“ count”递增。

我有:

x = 0
for x in range(1, 10):
if created_closed[0][x] == created_closed[1][x]:
    count = count + 1

# print(created_closed[0][1])
# print(created_closed[1][1])
print(count)

我尝试了两个带有注释的打印语句,它们都返回所需的值。现在,我想使用迭代器“ x”对每个列进行迭代以进行比较。除此之外,每对相同的增量计数。

这是我收到的错误:

Traceback (most recent call last):
File "c:/Users/U1111835/Documents/workday_data_analysis/analysis.py", line         29, in <module>
if created_closed[0][x] == created_closed[1][x]:
File "C:\Users\U1111835\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\series.py", line 767, in __getitem__
result = self.index.get_value(self, key)
File "C:\Users\U1111835\AppData\Local\Programs\Python\Python37-32\lib\site-  packages\pandas\core\indexes\base.py", line 3118, in get_value
tz=getattr(series.dtype, 'tz', None))
File "pandas\_libs\index.pyx", line 106, in pandas._libs.index.IndexEngine.get_value
File "pandas\_libs\index.pyx", line 114, in pandas._libs.index.IndexEngine.get_value
File "pandas\_libs\index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 958, in pandas._libs.hashtable.Int64HashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 964, in pandas._libs.hashtable.Int64HashTable.get_item
KeyError: 5

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题。

初始化created_closed时:

created_closed = pd.read_excel(excel_document, usecols="Q,R", header=None).head()

删除.head()以便读取整列