python - 在 for 循环中一次创建多个数据帧
问题描述
我有一个脚本可以检查 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)
这将返回一个数据框。理想情况下,我希望将多个数据框dst
以sheet_name
. 这里的问题是我不知道有多少(如果有的话)特征将具有空值。
我尝试为每个功能创建一个空白数据框,但我无法弄清楚在上面的代码块中如何使用它。
d = {dst.split("\\").pop().split(".")[2]: pd.DataFrame() for dst in Dst}
值得注意的是,这Dst
是一个 SQL 数据库的路径列表,并且DstKey
是我正在检查的每个数据库中的一个字段。
解决方案
考虑使用以dst作为键的数据帧字典,构建在循环外连接的数据帧内部列表:
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)
或者使用列表理解:
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)
对于使用数据框字典的 Excel 导出:
writer = pd.ExcelWriter('/path/to/output.xlsx')
for i, df in df_dict.items():
df.to_excel(writer, sheet_name=i)
writer.save()
推荐阅读
- rest - React Native 中的搜索栏未更新或过滤
- nuget-package - Visual Studio 2019 解决方案级别的“管理 Nuget 包”与项目级别的“管理 Nuget 包”有什么区别?
- swift - 如何使用 SwiftUI 将变量传递给 MFMailComposeViewController()?
- web-scraping - 网络抓取后未出现数据列(rvest 和 xlsx)
- angular - 模拟路由器的不正确实现
- python - 如何将 Pandas 数据帧的所有行与另一个 Pandas 数据帧中的单行相乘?
- angular - 在以角度加载数据之前调用的函数
- angular - Angular:如何在解析器完成之前更改 URL?
- batch-file - 当以 SYSTEM 用户(批处理)运行脚本时,更改当前登录的 Windows 用户的注册表值
- python - 有没有办法限制在 PyGame 中调用函数的次数?