我有一个脚本,可以检查GIS数据库中的要素是否存在字段中的缺失值。如果缺少该值,则将其附加到数据框,其中第一列为要素名称。理想情况下,我想将其拆分,以使特征名称成为图纸名称,但是我不确定如何迭代地进行。需要注意的是,并非每个功能都会有缺失值,在不同时间的不同功能可能有也可能没有缺失值,这就是进行此检查的目的。
df = pd.DataFrame()
for dst, dstkey in zip(Dst, DstKey):
with arcpy.da.SearchCursor(dst, ("OBJECTID", dstkey)) as cursor:
#returns an iterator of tuples
for row in cursor:
if (row[1] is None or not str(row[1]).strip()):
df = df.append(pd.DataFrame({dst.split("\\").pop(): str(row[0])}, index=[0]), ignore_index=True)
这将返回一个数据框。理想情况下,我想将多个数据框导出到Excel,并将dst
作为sheet_name
。这里的问题是,我不知道有多少个(如果有的话)功能具有空值。
我尝试为每个功能创建一个空白数据框,但我不知道如何在上面的代码块中利用它。
d = {dst.split("\\").pop().split(".")[2]: pd.DataFrame() for dst in Dst}
值得注意的是,Dst
是SQL数据库路径的列表,而DstKey
是我要检查的每个数据库中的字段。
答案 0 :(得分:1)
考虑使用以 dst 为键的数据帧字典来构建内部循环连接的数据帧的内部列表:
loading
或者具有列表理解功能:
df_dict = {}
for dst, dstkey in zip(Dst, DstKey):
inner = []
with arcpy.da.SearchCursor(dst, ("OBJECTID", dstkey)) as cursor:
# returns an iterator of tuples
for row in cursor:
if (row[1] is None or not str(row[1]).strip()):
inner.append(pd.DataFrame({dst.split("\\").pop(): str(row[0])}, index=[0])
df_dict[dstkey] = pd.concat(inner, ignore_index=True)
对于使用数据框字典的Excel导出:
df_dict = {}
for dst, dstkey in zip(Dst, DstKey):
with arcpy.da.SearchCursor(dst, ("OBJECTID", dstkey)) as cursor:
# returns an iterator of tuples
inner = [pd.DataFrame({dst.split("\\").pop(): str(row[0])}, index=[0])
for row in cursor if (row[1] is None or not str(row[1]).strip())]
df_dict[dstkey] = pd.concat(inner, ignore_index=True)