简体   繁体   English

结合 pandas 数据框和 ArcGIS 要素类

[英]Combining pandas dataframes and ArcGIS feature classes

I have a bit of a general question about the compatibility of Pandas dataframes and Arc featureclasses.关于 Pandas 数据帧和 Arc 要素类的兼容性,我有一个一般性的问题。

My current project is within ArcGIS and so I am mapping mostly with featureclasses.我当前的项目在 ArcGIS 中,因此我主要使用要素类进行映射。 I am however, most familiar with using pandas to perform simple data analysis with tables.然而,我最熟悉使用 pandas 对表格执行简单的数据分析。 Therefore, I am attempting to work with dataframes for the most part, and then join their data to feature classes for final mapping using some key field common between sets.因此,我大部分时间都在尝试使用数据框,然后使用集合之间常见的一些关键字段将它们的数据连接到要素类以进行最终映射。

Attempts:尝试:

1.I have come to find that arcpy AddJoin does not accept dfs. 1.我来发现arcpy AddJoin 不接受dfs。

2.I am currently trying convert df to csv and then do an Addjoin however I am unsure if this is supported and I far prefer the functionality of filtering dfs with "df.loc" etc. 2.我目前正在尝试将 df 转换为 csv 然后进行 Addjoin 但是我不确定这是否受支持,我更喜欢使用“df.loc”等过滤 dfs 的功能。

  1. Update cursor seems to be a good option, however, I am experiencing issues accessing the key field of the "row" in my loop to match records.更新 cursor 似乎是一个不错的选择,但是,我在访问循环中“行”的关键字段以匹配记录时遇到问题。 I will post another question about this as it is a separate issue.我将发布另一个关于此的问题,因为它是一个单独的问题。

Which of these or other options is the best for this purpose?这些或其他选项中的哪一个最适合此目的?

Thanks!谢谢!

Esri introduced something called Spatially Enabled DataFrame : Esri 引入了名为Spatially Enabled DataFrame 的东西:

The Spatially Enabled DataFrame inserts a custom namespace called spatial into the popular Pandas DataFrame structure to give it spatial abilities. Spatially Enabled DataFrame 将名为 spatial 的自定义命名空间插入到流行的 Pandas DataFrame 结构中,以赋予其空间能力。 This allows you to use intutive, pandorable operations on both the attribute and spatial columns.这允许您在属性列和空间列上使用直观的、可扩展的操作。

import arcpy
import pandas as pd

# important as it "enhances" Pandas by importing these classes
from arcgis.features import GeoAccessor, GeoSeriesAccessor
# from a shape file
df = pd.DataFrame.spatial.from_featureclass(r"data\hospitals.shp")
# from a map layer
project = arcpy.mp.ArcGISProject('CURRENT')
map = project.activeMap
first_layer = map.listLayers()[0]
layer_name = first_layer.name

df = pd.DataFrame.spatial.from_featureclass(layer_name)

# or directly by name
df = pd.DataFrame.spatial.from_featureclass("Streets")

# of if nested within a group layer (e.g. Buildings)
df = pd.DataFrame.spatial.from_featureclass("Buildings\Residential")
# save to shapefile
df.spatial.to_featureclass(location=r"c:\temp\residential_buildings.shp")

However, you have to use intermediate files if you go back and forth (to my knowledge).但是,如果您来回使用 go(据我所知),则必须使用中间文件。 Although it's a bit tricky having geopandas installed along arcpy , it may be worth looking into (only) using geopandas .尽管沿arcpy安装geopandas有点棘手,但可能值得(仅)使用geopandas进行研究。

IMHO, I would recommend that you avoid unnecessarily going back and forth between arcpy and pandas .恕我直言,我建议您避免在arcpypandas之间不必要地来回走动。 Pandas allows to merge, join and concat dataframes. Pandas 允许合并、连接和连接数据帧。 Or, you may be able to do everything in geopandas without needing to touch arcpy functions at all.或者,您可以在geopandas中做所有事情,而根本不需要接触arcpy函数。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM