-4

I want to convert the below text into a pandas dataframe. Is there a way I can use Python Pandas pre-built or in-built parser to convert? I can make a custom function for parsing but want to know if there is pre-built and/or fast solution.

In this example, the dataframe should result in two rows, one each of ABC & PQR

{
  "data": [
    {
      "ID": "ABC",
      "Col1": "ABC_C1",
      "Col2": "ABC_C2"
    },
    {
      "ID": "PQR",
      "Col1": "PQR_C1",
      "Col2": "PQR_C2"
    }
  ]
}
dsauce
  • 592
  • 2
  • 14
  • 36

1 Answers1

0

You've listed everything you need as tags. Use json.loads to get a dict from string

import json
import pandas as pd

d = json.loads('''{
  "data": [
    {
      "ID": "ABC",
      "Col1": "ABC_C1",
      "Col2": "ABC_C2"
    },
    {
      "ID": "PQR",
      "Col1": "PQR_C1",
      "Col2": "PQR_C2"
    }
  ]
}''')
df = pd.DataFrame(d['data'])
print(df)

Output:

    ID    Col1    Col2
0  ABC  ABC_C1  ABC_C2
1  PQR  PQR_C1  PQR_C2
bottledmind
  • 603
  • 3
  • 10