将嵌套字典解析为数据框 [英] Parsing nested dictionary to dataframe

查看:60
本文介绍了将嵌套字典解析为数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 JSON 文件创建数据框.

I am trying to create data frame from a JSON file.

并且每个 album_details 都有一个这样的嵌套字典

and each album_details have a nested dict like this

    {'api_path': '/albums/491200',
 'artist': {'api_path': '/artists/1421',
  'header_image_url': 'https://images.genius.com/f3a1149475f2406582e3531041680a3c.1000x800x1.jpg',
  'id': 1421,
  'image_url': 'https://images.genius.com/25d8a9c93ab97e9e6d5d1d9d36e64a53.1000x1000x1.jpg',
  'iq': 46112,
  'is_meme_verified': True,
  'is_verified': True,
  'name': 'Kendrick Lamar',
  'url': 'https://genius.com/artists/Kendrick-lamar'},
 'cover_art_url': 'https://images.genius.com/1efc5de2af228d2e49d91bd0dac4dc49.1000x1000x1.jpg',
 'full_title': 'good kid, m.A.A.d city (Deluxe Version) by Kendrick Lamar',
 'id': 491200,
 'name': 'good kid, m.A.A.d city (Deluxe Version)',
 'url': 'https://genius.com/albums/Kendrick-lamar/Good-kid-m-a-a-d-city-deluxe-version'}

我想在数据框中创建另一列,只有专辑名称,这是上面的dict

I want to create another column in the data frame with just album name which is one the above dict

'name': 'good kid, m.A.A.d city (Deluxe Version)',

我一直在寻找如何做到这一点很长一段时间,有人可以帮助我.谢谢

I have been looking how to do this from very long time , can some one please help me. thanks

推荐答案

是不是这样用str调用dict

df['name'] = df['album_details'].str['name']

这篇关于将嵌套字典解析为数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆