import pandas as pd
df = pd.read_excel("1.xls", None, header=None) # 读取数据表格
d = {"1001": ["4:,2:4", "4:,6:8"]}
count = 0
for h in d:
for hh in d[h]:
for i in df[h].iloc[hh]: # 循环找出有用的值
for j in df[h].iloc[hh][i]: # 遍历指定区域内的每一列
if j not in [0] and pd.notna(j):
# print("有数", str(j))
count += 1
break
if count == 0:
print("空表")
else:
print("有数")
程序的目的 判断一个框架表是否有数据
字典d 存储了iloc的参数 用来描述有效数据的位置 引用的时候报错
Traceback (most recent call last):
File "C:/Users/User/Desktop/·/find_blank_report.py", line 15, in <module>
for i in df[h].iloc[hh]: # 循环找出有用的值
File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexing.py", line 879, in __getitem__
return self._getitem_axis(maybe_callable, axis=axis)
File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexing.py", line 1493, in _getitem_axis
raise TypeError("Cannot index by location index with a non-integer key")
TypeError: Cannot index by location index with a non-integer key
大概是iloc不识别字典的值 输出第一个hh值为 4:,2:4 烦劳大神解答