首页 > 解决方案 > 如何将带有字典列的数据框转换为多级数据框

问题描述

我有 DataFrame,其中包含列中的字典。

可以如下创建

 lis = [
     {'id': '1', 
     'author': {'self': 'A', 
     'displayName': 'A'}, 
     'created': '2018-12-18', 
     'items': {'field': 'status', 
         'fromString': 'Backlog'}}, 
     {'id': '2', 
     'author': {'self': 'B', 
     'displayName': 'B'}, 
     'created': '2018-12-18', 
     'items': {'field': 'status', 
         'fromString': 'Funnel'}}] 

pd.DataFrame(lis)  

                              author     created id                                           items
0  {'self': 'A', 'displayName': 'A'}  2018-12-18  1  {'field': 'status', 'fromString': 'Backlog'}
1  {'self': 'B', 'displayName': 'B'}  2018-12-18  2   {'field': 'status', 'fromString': 'Funnel'}

我想将此信息转换为多级 DataFrame。

我一直在尝试

pd.MultiIndex.from_product(lis) 
pd.MultiIndex.from_frame(pd.DataFrame(lis))

但无法得到我正在寻找的结果。基本上我想要如下:

        author               created        id       items

self       displayName                             field   fromString
 A             A            2018-12-18       1      status   Backlog
 B             B            2018-12-18       2      status   Funnel

关于我如何实现这一目标的任何建议?

谢谢

标签: pythonpandasmulti-level

解决方案


您可以使用json.json_normalize- 但列名称用.分隔符展平:

from pandas.io.json import json_normalize

lis = [
     {'id': '1', 
     'author': {'self': 'A', 
     'displayName': 'A'}, 
     'created': '2018-12-18', 
     'items': {'field': 'status', 
         'fromString': 'Backlog'}}, 
     {'id': '2', 
     'author': {'self': 'B', 
     'displayName': 'B'}, 
     'created': '2018-12-18', 
     'items': {'field': 'status', 
         'fromString': 'Funnel'}}] 

df = json_normalize(lis)
print (df)
  id     created author.self author.displayName items.field items.fromString
0  1  2018-12-18           A                  A      status          Backlog
1  2  2018-12-18           B                  B      status           Funnel

对于MulitIndexin columns 和 in index - 首先Mulitiindex由所有没有.by的列创建DataFrame.set_index,然后使用str.split

df = df.set_index(['id','created'])
df.columns = df.columns.str.split('.', expand=True)
print (df)
              author               items           
                self displayName   field fromString
id created                                         
1  2018-12-18      A           A  status    Backlog
2  2018-12-18      B           B  status     Funnel

如果MulitIndex在列中需要 - 有可能,但会在列名中获取缺失值:

df.columns = df.columns.str.split('.', expand=True)
print (df)
   id     created author               items           
  NaN         NaN   self displayName   field fromString
0   1  2018-12-18      A           A  status    Backlog
1   2  2018-12-18      B           B  status     Funnel

缺失值应替换为空字符串:

df = df.rename(columns= lambda x: '' if x != x else x)
print (df)
  id     created author               items           
                   self displayName   field fromString
0  1  2018-12-18      A           A  status    Backlog
1  2  2018-12-18      B           B  status     Funnel

推荐阅读