0

I have a JSON file of the format

{
    "keyone": {
      "col1": "or",
      "col2": "abc",
      "col3": "bcd",
      "col4": "false"
    },
    "keytwo": {
      "col1": "aaa",
      "col2": "bbb",
      "col3": "ccc",
      "col4": "true"
    },
    "keythree": {
      "col1": "zor",
      "col2": "hhh",
      "col3": "lll",
      "col4": "false"
    }
  }

And I wanna convert in to Pandas dataframe suck that for each key we have a row with the required values

key       col1    col3 
keyone    or      bcd
keytwo    and     ccc
keythree  zor     lll

I have looked at JSON parsers but most of them are kinda complicated.

4
  • pd.DataFrame.from_dict(d,'index') Commented Feb 14, 2019 at 21:13
  • Is it JSON or is it a python dictionary? Commented Feb 14, 2019 at 21:13
  • It's in JSON @coldspeed Commented Feb 14, 2019 at 21:14
  • 2
    How does pd.read_json(json_data, orient='index') work for you? Commented Feb 14, 2019 at 21:14

2 Answers 2

8

You can read json data like below

import pandas as pd
df = pd.read_json("""{
    "keyone": {
      "col1": "or",
      "col2": "abc",
      "col3": "bcd",
      "col4": "false"
    },
    "keytwo": {
      "col1": "aaa",
      "col2": "bbb",
      "col3": "ccc",
      "col4": "true"
    },
    "keythree": {
      "col1": "zor",
      "col2": "hhh",
      "col3": "lll",
      "col4": "false"
    }
  }""")
df.T

OUTPUT

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

-1

You can use pandas to read the file in directly like

import pandas as pd    

df = pd.read_json("""{
    "keyone": {
      "col1": "or",
      "col2": "abc",
      "col3": "bcd",
      "col4": "false"
    },
    "keytwo": {
      "col1": "aaa",
      "col2": "bbb",
      "col3": "ccc",
      "col4": "true"
    },
    "keythree": {
      "col1": "zor",
      "col2": "hhh",
      "col3": "lll",
      "col4": "false"
    }
  }""")

df.T

1 Comment

How is this answer different from the other one posted?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.