简体   繁体   中英

pandas create a one row dataframe from nested dict

I know the "create pandas dataframe from nested dict" has a lot of entries here but I'm not found the answer that applies to my problem:

I have a dict like this:

{'id': 1,
 'creator_user_id': {'id': 12170254,
  'name': 'Nicolas',
  'email': 'some_mail@some_email_provider.com',
  'has_pic': 0,
  'pic_hash': None,
  'active_flag': True,
  'value': 12170254}....,

and after reading with pandas look like this:

df = pd.DataFrame.from_dict(my_dict,orient='index')
print(df)

id                                                                        1
creator_user_id           {'id': 12170254, 'name': 'Nicolas', 'email': '...
user_id                   {'id': 12264469, 'name': 'Daniela Giraldo G', ...
person_id                 {'active_flag': True, 'name': 'Cristina Cardoz...
org_id                    {'name': 'Cristina Cardozo', 'people_count': 1...
stage_id                                                                  2
title                                                      Cristina Cardozo

I would like to create a one-row dataframe where, for example, the nested creator_user_id column results in several columns that I after can name: creator_user_id_id, creator_user_id_name, etc.

thank you for your time!

Given you want one row, just use json_normalize()

pd.json_normalize({'id': 1,
 'creator_user_id': {'id': 12170254,
  'name': 'Nicolas',
  'email': 'some_mail@some_email_provider.com',
  'has_pic': 0,
  'pic_hash': None,
  'active_flag': True,
  'value': 12170254}})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM