Saya memiliki daftar kamus yang terlihat seperti berikut

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)]]}}]

Saya ingin datafame panda seperti berikut:

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)
0
emax 12 Mei 2021, 10:45

2 jawaban

Jawaban Terbaik

Anda dapat menguraikannya seperti:

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']))

Keluaran:

   raster_val                                           geometry
0  159.666672  POLYGON ((0.01664803578336149 5.81016448839316...
1  176.607101  POLYGON ((0.02497205367504218 5.81016448839316...
1
David Meu 12 Mei 2021, 08:40

Tidak jelas persis bagaimana Anda ingin koordinat disimpan di kolom geometri.

Dalam kode berikut saya telah menggunakan geometry dari shapely untuk mengonversinya menjadi 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...
1
norie 12 Mei 2021, 08:55