将带有嵌套列表的Geo json转换为pandas数据框。

7

I've a massive geo json in this form:

 {'features': [{'properties': {'MARKET': 'Albany',
    'geometry': {'coordinates': [[[-74.264948, 42.419877, 0],
       [-74.262041, 42.425856, 0],
       [-74.261175, 42.427631, 0],
       [-74.260384, 42.429253, 0]]],
     'type': 'Polygon'}}},
  {'properties': {'MARKET': 'Albany',
    'geometry': {'coordinates': [[[-73.929627, 42.078788, 0],
       [-73.929114, 42.081658, 0]]],
     'type': 'Polygon'}}},
  {'properties': {'MARKET': 'Albuquerque',
    'geometry': {'coordinates': [[[-74.769198, 43.114089, 0],
       [-74.76786, 43.114496, 0],
       [-74.766474, 43.114656, 0]]],
     'type': 'Polygon'}}}],
 'type': 'FeatureCollection'}

阅读 JSON 后:

import json
with open('x.json') as f:
    data = json.load(f)

我将数值读入列表,然后再转为数据框:

#to get a list of all markets
mkt=set([f['properties']['MARKET'] for f in data['features']])

#to create a list of market and associated lat long
markets=[(market,list(chain.from_iterable(f['geometry']['coordinates']))) for f in data['features'] for market in mkt if f['properties']['MARKET']==mkt]

df = pd.DataFrame(markets[0:], columns=['a','b'])     

数据框的前几行为:

      a       b
0   Albany  [[-74.264948, 42.419877, 0], [-74.262041, 42.4...
1   Albany  [[-73.929627, 42.078788, 0], [-73.929114, 42.0...
2   Albany  [[-74.769198, 43.114089, 0], [-74.76786, 43.11...

然后,为了展开列b中的嵌套列表,我使用了 pandas concat

df1 = pd.concat([df.iloc[:,0:1], df['b'].apply(pd.Series)], axis=1)

但是这样会创建8070列,其中许多列都是NaN。有没有一种方法可以通过市场(列a)对所有纬度和经度进行分组?需要一个由两列组成的百万行数据框。

期望输出如下:

mkt         lat         long 
Albany      42.419877   -74.264948
Albany      42.078788   -73.929627
..
Albuquerque  35.105361   -106.640342

请注意列表元素中的零 ([-74.769198, 43.114089, 0]) 需要被忽略。

请发布语法有效的JSON。最好将其格式化为“jq .”输出。 - J_H
@J_H 编辑了 JSON。这样好些吗? - skrubber
df = pd.DataFrame(gdf) # 将GeoDataFrame传递给pandas的DataFrame构造函数 df.head() - Saugat Rai
3个回答

9
像这样吗?
from pandas.io.json import json_normalize
df = json_normalize(geojson["features"])

coords = 'properties.geometry.coordinates'

df2 = (df[coords].apply(lambda r: [(i[0],i[1]) for i in r[0]])
           .apply(pd.Series).stack()
           .reset_index(level=1).rename(columns={0:coords,"level_1":"point"})
           .join(df.drop(coords,1), how='left')).reset_index(level=0)

df2[['lat','long']] = df2[coords].apply(pd.Series)

df2

输出:

   index  point properties.geometry.coordinates properties.MARKET  \
0      0      0         (-74.264948, 42.419877)            Albany   
1      0      1         (-74.262041, 42.425856)            Albany   
2      0      2         (-74.261175, 42.427631)            Albany   
3      0      3         (-74.260384, 42.429253)            Albany   
4      1      0         (-73.929627, 42.078788)            Albany   
5      1      1         (-73.929114, 42.081658)            Albany   
6      2      0         (-74.769198, 43.114089)       Albuquerque   
7      2      1          (-74.76786, 43.114496)       Albuquerque   
8      2      2         (-74.766474, 43.114656)       Albuquerque   

  properties.geometry.type        lat       long  
0                  Polygon -74.264948  42.419877  
1                  Polygon -74.262041  42.425856  
2                  Polygon -74.261175  42.427631  
3                  Polygon -74.260384  42.429253  
4                  Polygon -73.929627  42.078788  
5                  Polygon -73.929114  42.081658  
6                  Polygon -74.769198  43.114089  
7                  Polygon -74.767860  43.114496  
8                  Polygon -74.766474  43.114656 

如果:

geojson = {'features': [{'properties': {'MARKET': 'Albany',
    'geometry': {'coordinates': [[[-74.264948, 42.419877, 0],
       [-74.262041, 42.425856, 0],
       [-74.261175, 42.427631, 0],
       [-74.260384, 42.429253, 0]]],
     'type': 'Polygon'}}},
  {'properties': {'MARKET': 'Albany',
    'geometry': {'coordinates': [[[-73.929627, 42.078788, 0],
       [-73.929114, 42.081658, 0]]],
     'type': 'Polygon'}}},
  {'properties': {'MARKET': 'Albuquerque',
    'geometry': {'coordinates': [[[-74.769198, 43.114089, 0],
       [-74.76786, 43.114496, 0],
       [-74.766474, 43.114656, 0]]],
     'type': 'Polygon'}}}],
 'type': 'FeatureCollection'}

太好了,很高兴知道在reset_index()中可以指定级别。 - skrubber
@sharatpc 很高兴我能帮到你。 - Anton vBR
这个解决方案给了我一个错误:“模块”对象不可订阅。有任何想法为什么会出现这种情况? - Gobrel
1
@Gobrel 不好意思,您需要提供您的JSON样本。 - Anton vBR
@AntonvBR 我的问题中有一个示例。https://stackoverflow.com/questions/70422853/how-to-convert-geojson-with-multiple-nested-lists-to-pandas-dataframe - Gobrel

5

@Anton_vBR给出了一个很好的答案!

不过,也要考虑 "geopandas" 库作为另一个选择:

import geopandas

df = geopandas.read_file("yourfile.geojson")

df将是"class geopandas.GeoDataFrame",这将允许您像处理普通的pandas DataFrame一样操纵geojson(递归地通过内部结构)。


请在关于geopandas的更多细节中包含更多信息,例如如何在geopandas中完成提问者所要求的内容。 - asky
当然!我已经添加了足够理解的内容。谢谢你的评论,asky! - Kamal Barshevich

3
以上答案都很好,但这里有一些不同的东西。 Awkward Array 库(注意:我是作者)旨在大规模处理嵌套数据结构。偶然的是,在文档中,我使用了一个 GeoJSON 文件作为示例,不过我正在编写更多教程,使用更大的 Parquet 文件作为示例数据,与地理无关。
(这就是与 @kamal-barshevich 的 geopandas 答案不同之处:geopandas 是一个特定于领域的库,它“知道”地理信息,并且可能具有对该领域专家相关的功能。 Awkward Array 是一个用于操作数据结构的通用库,不知道任何有关地理的信息。)
我上面链接的文档演示了如何使用数组函数自身操作 GeoJSON 文件,而不需要 Pandas,从这里开始:
>>> import urllib.request
>>> import awkward as ak
>>> 
>>> url = "https://raw.githubusercontent.com/Chicago/osd-bike-routes/master/data/Bikeroutes.geojson"
>>> bikeroutes_json = urllib.request.urlopen(url).read()
>>> bikeroutes = ak.from_json(bikeroutes_json)
>>> bikeroutes
<Record ... [-87.7, 42], [-87.7, 42]]]}}]} type='{"type": string, "crs": {"type"...'>

但是在这个答案中,我会创建你想要的Pandas结构。ak.to_pandas函数将嵌套列表转换为MultiIndex。只对"features""geometry"下的"coordinates"应用它:

>>> bikeroutes.features.geometry.coordinates
<Array [[[[-87.8, 41.9], ... [-87.7, 42]]]] type='1061 * var * var * var * float64'>
>>> 
>>> ak.to_pandas(bikeroutes.features.geometry.coordinates)
                                              values
entry subentry subsubentry subsubsubentry           
0     0        0           0              -87.788573
                           1               41.923652
               1           0              -87.788646
                           1               41.923651
               2           0              -87.788845
...                                              ...
1060  0        8           1               41.950493
               9           0              -87.714819
                           1               41.950724
               10          0              -87.715284
                           1               41.951042

[96724 rows x 1 columns]

列表嵌套深度为三级,最后一级是经纬度对(例如[-87.788573, 41.923652])。您需要将它们放在不同的列中:
>>> bikeroutes.features.geometry.coordinates[..., 0]
<Array [[[-87.8, -87.8, ... -87.7, -87.7]]] type='1061 * var * var * float64'>
>>> bikeroutes.features.geometry.coordinates[..., 1]
<Array [[[41.9, 41.9, 41.9, ... 42, 42, 42]]] type='1061 * var * var * float64'>

这里使用类似于NumPy的切片(Awkward Array是NumPy的一种泛化),提取除最后一个维度外的所有维度 (...);第一个表达式提取项0(经度),第二个表达式提取项1(纬度)。 我们可以使用ak.zip将它们组合成一个新的记录类型,并赋予它们列名称。
>>> ak.to_pandas(ak.zip({
...     "longitude": bikeroutes.features.geometry.coordinates[..., 0],
...     "latitude": bikeroutes.features.geometry.coordinates[..., 1],
... }))
                            longitude   latitude
entry subentry subsubentry                      
0     0        0           -87.788573  41.923652
               1           -87.788646  41.923651
               2           -87.788845  41.923650
               3           -87.788951  41.923649
               4           -87.789092  41.923648
...                               ...        ...
1060  0        6           -87.714026  41.950199
               7           -87.714335  41.950388
               8           -87.714486  41.950493
               9           -87.714819  41.950724
               10          -87.715284  41.951042

[48362 rows x 2 columns]

这很接近你想要的内容。你最后想要的是将这些内容中的每一个与"features"中的一个"properties"相匹配。我的GeoJSON文件中没有"MARKET"

>>> bikeroutes.features.properties.type
1061 * {"STREET": string, "TYPE": string, "BIKEROUTE": string, "F_STREET": string, "T_STREET": option[string]}

但是"STREET"可能是一个很好的替代。这些属性位于比坐标更深层的级别上:

>>> bikeroutes.features.geometry.coordinates[..., 0].type
1061 * var * var * float64
>>> bikeroutes.features.properties.STREET.type
1061 * string

经纬度比街道名称深入两个嵌套列表级别,但是 ak.zip 将它们向下广播(类似于NumPy的广播概念,需要针对变长列表的必要扩展)。
最终表达式为:
>>> ak.to_pandas(ak.zip({
...     "longitude": bikeroutes.features.geometry.coordinates[..., 0],
...     "latitude": bikeroutes.features.geometry.coordinates[..., 1],
...     "street": bikeroutes.features.properties.STREET,
... }))
                            longitude   latitude           street
entry subentry subsubentry                                       
0     0        0           -87.788573  41.923652  W FULLERTON AVE
               1           -87.788646  41.923651  W FULLERTON AVE
               2           -87.788845  41.923650  W FULLERTON AVE
               3           -87.788951  41.923649  W FULLERTON AVE
               4           -87.789092  41.923648  W FULLERTON AVE
...                               ...        ...              ...
1060  0        6           -87.714026  41.950199     N ELSTON AVE
               7           -87.714335  41.950388     N ELSTON AVE
               8           -87.714486  41.950493     N ELSTON AVE
               9           -87.714819  41.950724     N ELSTON AVE
               10          -87.715284  41.951042     N ELSTON AVE

[48362 rows x 3 columns]

如果您只想将市场与经纬度点联系起来,可以忽略MultiIndex,或者您可以使用Pandas函数将该MultiIndex的组件转换为列。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接