1

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.

2
  • Have you tried anything ? What's the difficult part for this ? Commented Jul 4, 2022 at 21:10
  • Welcome to Stack Overflow! You seem to be asking for someone to write some code for you. Stack Overflow is a question and answer site, not a code-writing service. Please see here to learn how to write effective questions. A good way to demonstrate this effort is to include the code you've written so far. Commented Jul 4, 2022 at 21:11

2 Answers 2

1

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
          ]
        ]
      ]
    }
  ]
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.