繁体   English   中英

Python:如何将字典列表转换为地理熊猫 dataframe?

[英]Python: how to convert a list of dictionary to a geopandas dataframe?

我的字典列表如下所示

myList:

[{'properties': {'raster_val': 159.6666717529297},
  'geometry': {'type': 'Polygon',
   'coordinates': [[(0.016648035783361492, 5.810164488393165),
     (0.016648035783361492, 5.801840470501484),
     (0.024972053675042183, 5.801840470501484),
     (0.024972053675042183, 5.810164488393165),
     (0.016648035783361492, 5.810164488393165)]]}},
 {'properties': {'raster_val': 176.6071014404297},
  'geometry': {'type': 'Polygon',
   'coordinates': [[(0.024972053675042183, 5.810164488393165),
     (0.024972053675042183, 5.801840470501484),
     (0.033296071566722985, 5.801840470501484),
     (0.033296071566722985, 5.810164488393165),
     (0.024972053675042183, 5.810164488393165)]]}}]

我想要一个 pandas 数据名称,如下所示

df
      raster_val            geometry
0   159.6666717529297     POLYGON(0.016648035783361492, 5.810164488393165), (0.016648035783361492, 5.801840470501484),(0.024972053675042183, 5.801840470501484),(0.024972053675042183, 5.810164488393165),(0.016648035783361492, 5.810164488393165)
1   176.6071014404297     POLYGON(0.024972053675042183, 5.810164488393165),(0.024972053675042183, 5.801840470501484), (0.033296071566722985, 5.801840470501484),(0.033296071566722985, 5.810164488393165),(0.024972053675042183, 5.810164488393165)

目前尚不清楚您希望如何将坐标存储在几何列中。

在下面的代码中,我使用shapely中的geometry将它们转换为POLYGON

import pandas as pd
from shapely import geometry

mylist = [{'properties': {'raster_val': 159.6666717529297},
  'geometry': {'type': 'Polygon',
   'coordinates': [[(0.016648035783361492, 5.810164488393165),
     (0.016648035783361492, 5.801840470501484),
     (0.024972053675042183, 5.801840470501484),
     (0.024972053675042183, 5.810164488393165),
     (0.016648035783361492, 5.810164488393165)]]}},
 {'properties': {'raster_val': 176.6071014404297},
  'geometry': {'type': 'Polygon',
   'coordinates': [[(0.024972053675042183, 5.810164488393165),
     (0.024972053675042183, 5.801840470501484),
     (0.033296071566722985, 5.801840470501484),
     (0.033296071566722985, 5.810164488393165),
     (0.024972053675042183, 5.810164488393165)]]}}]

mydic = {poly['properties']['raster_val']: geometry.Polygon(poly['geometry']['coordinates'][0]) for poly in mylist}

df = pd.DataFrame.from_dict(mydic, orient='index').reset_index()

df.columns = ['raster_val','geometry']

print(df)

   raster_val                                           geometry
0  159.666672  POLYGON ((0.01664803578336149 5.81016448839316...
1  176.607101  POLYGON ((0.02497205367504218 5.81016448839316...

你可以像这样解析它:

import pandas as pd
from shapely.geometry import Polygon

data = [{'properties': {'raster_val': 159.6666717529297},
         'geometry': {'type': 'Polygon',
                      'coordinates': [[(0.016648035783361492, 5.810164488393165),
                                       (0.016648035783361492, 5.801840470501484),
                                       (0.024972053675042183, 5.801840470501484),
                                       (0.024972053675042183, 5.810164488393165),
                                       (0.016648035783361492, 5.810164488393165)]]}},
        {'properties': {'raster_val': 176.6071014404297},
         'geometry': {'type': 'Polygon',
                      'coordinates': [[(0.024972053675042183, 5.810164488393165),
                                       (0.024972053675042183, 5.801840470501484),
                                       (0.033296071566722985, 5.801840470501484),
                                       (0.033296071566722985, 5.810164488393165),
                                       (0.024972053675042183, 5.810164488393165)]]}}]

parsed_data = [[item['properties']['raster_val'],
                Polygon(item['geometry']['coordinates'][0])] for item in data]
print(pd.DataFrame(data=parsed_data, columns=['raster_val', 'geometry']))

Output:

   raster_val                                           geometry
0  159.666672  POLYGON ((0.01664803578336149 5.81016448839316...
1  176.607101  POLYGON ((0.02497205367504218 5.81016448839316...

暂无
暂无

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

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