scott martin
scott martin

Reputation: 1293

Pandas - Extracting elements within a dictionary

I have a Python dictionary in the below structure. I am trying to extract certain elements from the Dictionary and convert them to a Dataframe.

When I try to perform pd.Dataframe(df) I get summary of the 2 groups data and PageCount whereas I only want the elements within Output in the Dataframe

{'code': 200,
 'data': {'Output': [
  {'id': 58,
   'title': 'title1'},
  {'id': 59,
   'title': 'title2'}],
'PageCount': {'count': 196,
'page': 1,
'perPage': 10,
'totalPages': 20}},
'request_id': 'fggfgggdgd'}

Expected output:

id, title
58, title1
59, title2

Upvotes: 1

Views: 30

Answers (2)

anky
anky

Reputation: 75080

You can also use;

l=[v['Output'] for k,v in d.items() if isinstance(v,dict) & ('Output' in str(v))]
pd.DataFrame(l[0])

   id   title
0  58  title1
1  59  title2

Upvotes: 1

thushv89
thushv89

Reputation: 11333

You can do,

df = pd.io.json.json_normalize(dct["data"]["Output"])

Upvotes: 2

Related Questions