簡體   English   中英

如何基於Pandas DataFrame創建.json文件? Python

[英]How to create .json file based on Pandas DataFrame? Python

我有帶有 ID、緯度和經度的 Pandas DataFrame:

    Id      Latitude    Longitude   
0   01    -17.658200    36.123498   
1   01    -17.658151    36.123522
2   01    -17.658063    36.123576
3   02    -11.896096    30.388277   
4   02    -11.896096    30.388277   
5   02    -11.896088    30.388275

我想從 this.json 文件創建(這樣的格式,因為它必須被 Sentinelhub 接受)

以下是 Sentinelhub 接受的 json 文件示例:

{'type': 'FeatureCollection',
 'features': [{'type': 'Feature',
   'properties': {},
   'geometry': {'type': 'Polygon',
    'coordinates': [[[0.57952880859375, 20.037870053952016],
      [0.43121337890625, 20.035289711352377],
      [0.43121337890625, 19.93720533223859]]]}}]}

因此,對於我的小示例,所需的 output 應該看起來像這樣(它是兩個結構化的,因為我們有 2 個位置(多邊形)定義為 ID:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -17.658200,
               36.123498
            ],
            [
              -17.658151,
               36.123522
            ],
            [
              -17.658063,
               36.123576
            ]
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -11.896096,
               30.388277
            ],
            [
              -11.896096,
               30.388277
            ],
            [
              -11.896088,
               30.388275
            ]
          ]
        ]
      }
    }
  ]
}

如您所見,固定元素對每個 ID 重復。

我對使用這種類型的數據完全陌生,所以到目前為止我設法做的是創建一個像這樣的字典:

places = {}
    for row in df.itertuples():
        if row.Id not in places:
            places[row.Id] = [[row.Latitude, row.Longitude]]
        else:
            places[row.Id].append([row.Latitude, row.Longitude])

這導致我在字典中按 ID 分隔坐標...:

{01: [[-17.658200,36.123498],
      [-17.658151,36.123522],
      [-17.658063,36.123576]],
 02: [[-11.896096,30.388277],
     [-11.896096,30.388277],
     [-11.896088,30.388275]}

您可以嘗試groupby

def f(x):
    return {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Polygon",
        "coordinates": x[["Latitude", "Longitude"]].values.tolist()
      }
    }
out = {
    "type": "FeatureCollection",
    "features": df.groupby("Id").apply(f).to_list()
}

說明:

  1. 使用groupbyid對行進行分組: df.groupby("Id")
  2. 在每一行上應用一個自定義 function 來構建一個“功能”項目: df.groupby("Id").apply(f)
  3. 使用to_list將 output 轉換為列表: df.groupby("Id").apply(f).to_list()
  4. 在 output 字典中集成 output:
out = {
    "type": "FeatureCollection",
    "features": df.groupby("Id").apply(f).to_list()
}

Output:

{
    'type': 'FeatureCollection', 
    'features': [
        {
            'type': 'Feature', 
            'properties': {}, 
            'geometry': {
                'type': 'Polygon', 
                'coordinates': [[-17.6582, 36.123498], [-17.658151, 36.123522], [-17.658063, 36.123576]]
            }
        }, 
        {
            'type': 'Feature', 
            'properties': {}, 
            'geometry': {
                'type': 'Polygon', 
                'coordinates': [[-11.896096, 30.388277], [-11.896096, 30.388277], [-11.896088, 30.388275]]
            }
        }
    ]
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM