V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
cqcn1991
V2EX  ›  问与答

想问下 Python Pandas 读 nested json 的处理方式?

  •  
  •   cqcn1991 · 2015-07-28 22:24:49 +08:00 · 2522 次点击
    这是一个创建于 3220 天前的主题,其中的信息可能已经有所发展或是发生改变。

    比如这样一个数据组

    {u'detail_url': u'http://www.movieinsider.com/m4032/open-grave-/',
     u'douban_info': {u'aka': [u'\u5929\u5751\u575f\u5730',
                               u'\u655e\u5f00\u7684\u575f\u5893'],
                      u'alt': u'http://movie.douban.com/subject/13899371/',
                      u'casts': [{u'alt': u'http://movie.douban.com/celebrity/1314499/',
                                  u'avatars': {u'large': u'http://img4.douban.com/img/celebrity/large/46059.jpg',
                                               u'medium': u'http://img4.douban.com/img/celebrity/medium/46059.jpg',
                                               u'small': u'http://img4.douban.com/img/celebrity/small/46059.jpg'},
                                  u'id': u'1314499',
                                  u'name': u'\u7ea6\u745f\u592b\xb7\u6469\u6839'},
                                 {u'alt': u'http://movie.douban.com/celebrity/1036300/',
                                  u'avatars': {u'large': u'http://img4.douban.com/img/celebrity/large/1393386887.76.jpg',
                                               u'medium': u'http://img4.douban.com/img/celebrity/medium/1393386887.76.jpg',
                                               u'small': u'http://img4.douban.com/img/celebrity/small/1393386887.76.jpg'},
                                  u'id': u'1036300',
                                  u'name': u'\u6c99\u5c14\u6258\xb7\u79d1\u666e\u96f7'},
                                 {u'alt': u'http://movie.douban.com/celebrity/1049595/',
                                  u'avatars': {u'large': u'http://img3.douban.com/img/celebrity/large/1377843669.55.jpg',
                                               u'medium': u'http://img3.douban.com/img/celebrity/medium/1377843669.55.jpg',
                                               u'small': u'http://img3.douban.com/img/celebrity/small/1377843669.55.jpg'},
                                  u'id': u'1049595',
                                  u'name': u'\u6258\u9a6c\u65af\xb7\u514b\u83b1\u8212\u66fc'},
                                 {u'alt': u'http://movie.douban.com/celebrity/1318450/',
                                  u'avatars': {u'large': u'http://img3.douban.com/img/celebrity/large/43010.jpg',
                                               u'medium': u'http://img3.douban.com/img/celebrity/medium/43010.jpg',
                                               u'small': u'http://img3.douban.com/img/celebrity/small/43010.jpg'},
                                  u'id': u'1318450',
                                  u'name': u'\u827e\u7433\xb7\u7406\u67e5\u5179'}],
                      u'collect_count': 1507,
                      u'comments_count': 468,
                      u'countries': [u'\u7f8e\u56fd'],
                      u'current_season': None,
                      u'directors': [{u'alt': u'http://movie.douban.com/celebrity/1302444/',
                                      u'avatars': {u'large': u'http://img3.douban.com/img/celebrity/large/45953.jpg',
                                                   u'medium': u'http://img3.douban.com/img/celebrity/medium/45953.jpg',
                                                   u'small': u'http://img3.douban.com/img/celebrity/small/45953.jpg'},
                                      u'id': u'1302444',
                                      u'name': u'\u5188\u624e\u7f57\xb7\u6d1b\u4f69\u5179-\u52a0\u52d2\u679c'}],
                      u'do_count': None,
                      u'douban_site': u'',
                      u'episodes_count': None,
                      u'genres': [u'\u6050\u6016'],
                      u'id': u'13899371',
                      u'images': {u'large': u'http://img3.douban.com/view/movie_poster_cover/lpst/public/p2162073011.jpg',
                                  u'medium': u'http://img3.douban.com/view/movie_poster_cover/spst/public/p2162073011.jpg',
                                  u'small': u'http://img3.douban.com/view/movie_poster_cover/ipst/public/p2162073011.jpg'},
                      u'mobile_url': u'http://movie.douban.com/subject/13899371/mobile',
                      u'original_title': u'Open Grave',
                      u'rating': {u'average': 5.6,
                                  u'max': 10,
                                  u'min': 0,
                                  u'stars': u'30'},
                      u'ratings_count': 1283,
                      u'reviews_count': 6,
                      u'schedule_url': u'',
                      u'seasons_count': None,
                      u'subtype': u'movie',
                      u'summary': u'\u672c\u8bb2\u8ff0\u4e86\u67d0\u4e2a\u504f\u50fb\u8352\u51c9\u7684\u68ee\u6797\u91cc\uff0c\u516d\u4e2a\u4eba\u5728\u4e00\u5904\u6709\u8150\u5c38\u7684\u9732\u5929\u575f\u573a\u65c1\u9192\u6765\uff0c\u5374\u597d\u50cf\u5f97\u4e86\u5065\u5fd8\u4e00\u822c\u7684\u6050\u6016\u95f9\u9b3c\u6545\u4e8b\u3002\u4ed6\u4eec\u65e0\u5904\u53ef\u53bb\uff0c\u88ab\u8feb\u628a\u795e\u79d8\u4e8b\u4ef6\u7684\u7ebf\u7d22\u62fc\u51d1\u8d77\u6765\uff0c\u6700\u7ec8\u5c06\u5e26\u9886\u4ed6\u4eec\u8d70\u8fdb\u60ca\u4eba\u7684\u7ed3\u5c40\uff0c\u800c\u4e0d\u81f3\u4e8e\u4f7f\u771f\u76f8\u6765\u7684\u592a\u665a\u3002',
                      u'title': u'\u5f00\u68fa',
                      u'wish_count': 640,
                      u'year': u'2013'},
     u'movie_tt_id': u'tt2071550',
     u'name': u'Open Grave ',
     u'omdb_info': {u'Actors': u'Sharlto Copley, Thomas Kretschmann, Josie Ho, Joseph Morgan',
                    u'Awards': u'2 nominations.',
                    u'Country': u'USA',
                    u'Director': u'Gonzalo L\xf3pez-Gallego',
                    u'Genre': u'Horror, Mystery, Thriller',
                    u'Language': u'English',
                    u'Metascore': u'33',
                    u'Plot': u'A man wakes up in the wilderness, in a pit full of dead bodies, with no memory and must determine if the murderer is one of the strangers who rescued him, or if he himself is the killer.',
                    u'Poster': u'http://ia.media-imdb.com/images/M/MV5BMTc1MDM5MTI0Ml5BMl5BanBnXkFtZTgwOTMyODI1MDE@._V1_SX300.jpg',
                    u'Rated': u'R',
                    u'Released': u'3 Jan 2014',
                    u'Response': u'True',
                    u'Runtime': u'102 min',
                    u'Title': u'Open Grave',
                    u'Type': u'movie',
                    u'Writer': u'Eddie Borey, Chris Borey',
                    u'Year': u'2013',
                    u'imdbID': u'tt2071550',
                    u'imdbRating': u'6.3',
                    u'imdbVotes': u'18896'}}
    

    觉得主要有两种处理方式
    1. 把nested里面需要的数据,直接读成column
    2. 把nested的数据再读成dataframe, 然后merge进来

    不知道具体怎么做?我自己做碰到了几个问题:
    对于第1个做法

    import json
    from pprint import pprint
    import pandas as pd
    from pandas import Series,DataFrame
    
    with open('..\lib\movie_list_2014_v2.json') as data_file:    
        data = json.load(data_file)
    
    pd_data = DataFrame(data)
    pd_data['imdb_rating']=pd_data['omdb_info']['imdbRating']
    

    这样子读,pd_data['omdb_info']['imdbRating']会报错

    对于第2个做法,不知道怎么弄....我看书好像是得手动转成表,好像很麻烦的样子...

    4 条回复    2015-07-28 23:24:47 +08:00
    ligyxy
        1
    ligyxy  
       2015-07-28 22:29:50 +08:00
    转成表有什么好处?如果是我我就继续用dict来存数据。
    cqcn1991
        2
    cqcn1991  
    OP
       2015-07-28 22:36:08 +08:00
    @ligyxy 想用Pandas来处理数据啊....
    ligyxy
        3
    ligyxy  
       2015-07-28 23:02:29 +08:00
    我想用你说的第一个方法比较直接,我运行pd_data['omdb_info']['imdbRating']并没有出错

    但是pd_data['omdb_info']['imdbRating']是一个样本的一个变量,赋给pd_data['imdb_rating']恐怕不是你想要的,用另外一个数据框来逐行储存各个样本的需要的变量比较妥当吧
    cqcn1991
        4
    cqcn1991  
    OP
       2015-07-28 23:24:47 +08:00
    @ligyxy 话说能写一下具体语法么?查了一下有.iterow()的方法,但是不太会写
    `pd_data['omdb_info'].ix[3]['imdbRating']`就能显示第3个movie的`imdbRating`
    但是怎么把这个数据提取出来,存成新的一列column? 其实就是个循环历遍每个row,然而实在不会
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3034 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 04:29 · PVG 12:29 · LAX 21:29 · JFK 00:29
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.