2017-02-09 8 views
0

インデックスが最初のレベル{key:value}を形成し、インデックスの下の行がネストされたdictを形成する次のmultiindexデータフレームからネストされたdictのリストを作成しようとしています。DataFrameからネストされたdictのリストを作成していますか?

[{'date':1980, 'country': 'United States', 'country_id':840 
    'count':42, 'players' : [{'player_name: xxxx','ranking': 46, 'hand': 'r'}, {'player_name: yyy', 'ranking':20, 'hand': 'r'}...]}, 
{'date':1980, 'country': 'Czech Republic', 'country_id':203, 
    'count':42, 'players' : [{'player_name: xxxx','ranking': 46, 'hand':'r'}, 
    {'player_name: yyy', 'ranking':20, 'hand': 'r'}...]},... 
{'date':1982, 'country': 'United States', 'country_id':840, 
    'count':42, 'players' : [{'player_name: xxxx','ranking': 46, 'hand': 'r'},...] 


             HAND RANKING   PLAYER_NAME 
DATE COUNTRY  COUNTRY_ID COUNT         
1980 United States  840  42   R  46  Tim Gullikson 
            42   L  96  Nick Saviano 
            42   L  3  Jimmy Connors 
            42   L  79  Bruce Manson 
    Czech Republic 203  2   R  23   Tomas Smid 
            2   R  65  Pavel Slozil 
    New Zealand  554  3   R  66  Chris Lewis NZL 
      . 
      . 
1982 United States  840  42   L  46  Van Winitsky 
            42   R  24  Steve Denton 
            42   R  26   Mel Purcell         
            3   R  76  Russell Simpson 

      . 
      .    

答えて

0

組み合わせgroupbyの、to_dict('records')、および他のニュアンス

lod = [] 
for name, group in df.groupby(level=[0, 1, 2, 3]): 
    d = dict(zip(df.index.names, name)) 
    d['players'] = group.to_dict('records') 
    lod.append(d) 

lod 

[{'count': 2, 
    'country': 'Czech Republic', 
    'country_id': 203, 
    'date': 1980, 
    'players': [{'HAND': 'R', 'PLAYER_NAME': 'Tomas Smid', 'RANKING': 23}, 
    {'HAND': 'R', 'PLAYER_NAME': 'Pavel Slozil', 'RANKING': 65}]}, 
{'count': 3, 
    'country': 'New Zealand', 
    'country_id': 554, 
    'date': 1980, 
    'players': [{'HAND': 'R', 'PLAYER_NAME': 'Chris Lewis NZL', 'RANKING': 66}]}, 
{'count': 42, 
    'country': 'United States', 
    'country_id': 840, 
    'date': 1980, 
    'players': [{'HAND': 'R', 'PLAYER_NAME': 'Tim Gullikson', 'RANKING': 46}, 
    {'HAND': 'L', 'PLAYER_NAME': 'Nick Saviano', 'RANKING': 96}, 
    {'HAND': 'L', 'PLAYER_NAME': 'Jimmy Connors', 'RANKING': 3}, 
    {'HAND': 'L', 'PLAYER_NAME': 'Bruce Manson', 'RANKING': 79}]}, 
{'count': 3, 
    'country': 'United States', 
    'country_id': 840, 
    'date': 1982, 
    'players': [{'HAND': 'R', 'PLAYER_NAME': 'Russell Simpson', 'RANKING': 76}]}, 
{'count': 42, 
    'country': 'United States', 
    'country_id': 840, 
    'date': 1982, 
    'players': [{'HAND': 'L', 'PLAYER_NAME': 'Van Winitsky', 'RANKING': 46}, 
    {'HAND': 'R', 'PLAYER_NAME': 'Steve Denton', 'RANKING': 24}, 
    {'HAND': 'R', 'PLAYER_NAME': 'Mel Purcell', 'RANKING': 26}]}] 
関連する問題