Home > Software design >  How to read JSON data WITHOUT unnesting, into pandas df
How to read JSON data WITHOUT unnesting, into pandas df

Time:12-28

I have a JSON object like so:

 {
        "a": "x",
        "b": "y",
        "c": "z",
        "d": {
            "d1": "1234",
            "d2": "Team E",
            "d3": "Team D"
        }
      }

I want to read it into a dataframe in pandas without unnesting anything in column D. The new df should look like this:

a    b    c      d
x    y    z    {"d1": "1234","d2": "Team E","d3": "Team D"}

How do I go about this? Ive tried pd.read_json(), pd.DataFrame.from_dict(), pd.DataFrame and all these are parsing the data I dont want parsed into columns.

CodePudding user response:

Try:

data = {
    "a": "x",
    "b": "y",
    "c": "z",
    "d": {"d1": "1234", "d2": "Team E", "d3": "Team D"},
}

df = pd.DataFrame([data])
print(df)

Prints:

   a  b  c                                               d
0  x  y  z  {'d1': '1234', 'd2': 'Team E', 'd3': 'Team D'}
  • Related