Home > database >  Convert to DataFrame Pandas to json multiple level python
Convert to DataFrame Pandas to json multiple level python

Time:07-05

Is there any way to convert a pandas dataframe into a json with different levels, I have this dataframe:

df = pd.DataFrame([
              {"type":"Feature","Id":319,"Departament":"1 DE MAYO","State":"CHACO","coordinates":[[[-58.95370956800002, -26.87059472200002]]]},
              {"type":"Feature","Id":320,"Departament":"12 DE OCTUBRE","State":"CHACO","coordinates":[[[-58.95370956800002, -26.87059472200002]]]},
              {"type":"Feature","Id":314,"Departament":"2 DE ABRIL","State":"CHACO","coordinates":[[[-58.95370956800002, -26.87059472200002]]]},
              {"type":"Feature","Id":308,"Departament":"25 DE MAYO","State":"CHACO","coordinates":[[[-58.95370956800002, -26.87059472200002]]]},
              {"type":"Feature","Id":100,"Departament":"25 DE MAYO","State":"CHACO","coordinates":[[[-58.95370956800002, -26.87059472200002]]]}])

I really want an output like so:

"features": [
 {
  "type": "Feature",
  "properties": {
   "id": 319,
   "Departament": "1 DE MAYO",       
   "State": "CHACO"
  },
  "geometry": {
   "coordinates": [
    [
     [
      -58.32487869300002,
      -30.838373183999977
     ]
    ]
  ]
  }
 }
]

}

Thanks for your help i hope i was clear.

CodePudding user response:

You can use:

import json

def to_json(row):
    return {'type': row.iloc[0],
            'properties': row.iloc[1:-1].to_dict(),
            'geometry': row.iloc[-1]}
data = {'features': df.apply(to_json, axis=1).to_list()}

print(json.dumps(data, indent=2))

Output:

{
  "features": [
    {
      "type": "Feature",
      "properties": {
        "Id": 319,
        "Departament": "1 DE MAYO",
        "State": "CHACO"
      },
      "geometry": [
        [
          [
            -58.95370956800002,
            -26.87059472200002
          ]
        ]
      ]
    },
    {
      "type": "Feature",
      "properties": {
        "Id": 320,
        "Departament": "12 DE OCTUBRE",
        "State": "CHACO"
      },
      "geometry": [
        [
          [
            -58.95370956800002,
            -26.87059472200002
          ]
        ]
      ]
    },
    {
      "type": "Feature",
      "properties": {
        "Id": 314,
        "Departament": "2 DE ABRIL",
        "State": "CHACO"
      },
      "geometry": [
        [
          [
            -58.95370956800002,
            -26.87059472200002
          ]
        ]
      ]
    },
    {
      "type": "Feature",
      "properties": {
        "Id": 308,
        "Departament": "25 DE MAYO",
        "State": "CHACO"
      },
      "geometry": [
        [
          [
            -58.95370956800002,
            -26.87059472200002
          ]
        ]
      ]
    },
    {
      "type": "Feature",
      "properties": {
        "Id": 100,
        "Departament": "25 DE MAYO",
        "State": "CHACO"
      },
      "geometry": [
        [
          [
            -58.95370956800002,
            -26.87059472200002
          ]
        ]
      ]
    }
  ]
}

CodePudding user response:

You can use the dataframes’ .to_json() method.

  • Related