从DataFrame创建嵌套dict列表?

时间:2017-02-09 01:27:27

标签: pandas dictionary dataframe nested

我正在尝试从以下多索引数据框创建嵌套dict列表,其中索引形成第一级{key:value},索引下的行形成嵌套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 

             .
             .            

1 个答案:

答案 0 :(得分:0)

groupbyto_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}]}]