【问题标题】:How to turn nested dictionary into pandas dataframe?如何将嵌套字典变成熊猫数据框?
【发布时间】:2021-04-01 11:42:59
【问题描述】:

我有这个嵌套字典:

{'attrs': ('LA', 'E', 'Can', 'AP', 'ME', 'A', 'M', 'Car', 'US'),
 'self': {'ac': {'AP', 'Can', 'Car', 'E', 'LA', 'M', 'ME', 'US'},
  'anz': {'AP', 'E', 'US'},
  'ana': {'AP', 'E', 'US'},
  'aa': {'AP'},
  'taag': {'A', 'AP', 'Can', 'E', 'ME', 'US'},
  'bm': {'E'},
  'l': {'A', 'AP', 'Can', 'E', 'LA', 'M', 'ME', 'US'},
  'm': {'Can', 'Car', 'LA', 'M', 'US'},
  'sca': {'A', 'AP', 'E', 'LA', 'US'},
  'sia': {'A', 'AP', 'Can', 'E', 'ME', 'US'},
  'tai': {'AP', 'Car', 'E', 'LA', 'US'},
  'ua': {'AP', 'Can', 'Car', 'E', 'LA', 'M', 'US'},
  'v': {'A', 'AP', 'E', 'LA', 'M', 'US'}}}

这就是我构建它的方式:

build_context = lambda objects, attributes, table : {'attrs' : tuple(attributes), 'self' : {object : {attributes[i] for i in range(len(row)) if row[i]} for (object, row) in zip(objects, table)}}


context = build_context(objects =
('ac', 'anz', 'ana', 'aa', 'taag', 'bm', 'l', 'm', 'sca', 'sia', 'tai',
'ua', 'v'),
attributes = ('LA', 'E', 'Can', 'AP', 'ME', 'A', 'M', 'Car', 'US'),
table = ((True,True,True,True,True,False,True,True,True),
(False,True,False,True,False,False,False,False,True), (False,True,False,True,False,False,False,False,True),
(False,False,False,True,False,False,False,False,False),
(False,True,True,True,True,True,False,False,True),
(False,True,False,False,False,False,False,False,False),
(True,True,True,True,True,True,True,False,True),
(True,False,True,False,False,False,True,True,True), (True,True,False,True,False,True,False,False,True),
(False,True,True,True,True,True,False,False,True),
(True,True,False,True,False,False,False,True,True),
(True,True,True,True,False,False,True,True,True),
(True,True,False,True,False,True,True,False,True)))

如何把它变成熊猫数据框?它应该看起来像这样,但我在代码中使用了缩写:

【问题讨论】:

  • 请提供预期的输出格式
  • 你能提供一些预期输出/数据帧的样本吗?
  • @anon01 是的,我添加了它
  • @IoaTzimas 是的,我添加了它

标签: python python-3.x pandas dictionary


【解决方案1】:

这是我的解决方案:

attributes = ('LA', 'E', 'Can', 'AP', 'ME', 'A', 'M', 'Car', 'US')

data=d['self']

new_data=[]

for i in data:
    l={}
    for k in attributes:
        if k in data[i]:
            l[k]=1
        else:
            l[k]=0
    new_data.append(l)

res=pd.DataFrame_from_dict(new_data, orient='columns')

res['company']=data.keys()

res=res[['company', 'LA', 'E', 'Can', 'AP', 'ME', 'A', 'M', 'Car', 'US']]

print(res)

输出:

   company  LA  E  Can  AP  ME  A  M  Car  US
0       ac   1  1    1   1   1  0  1    1   1
1      anz   0  1    0   1   0  0  0    0   1
2      ana   0  1    0   1   0  0  0    0   1
3       aa   0  0    0   1   0  0  0    0   0
4     taag   0  1    1   1   1  1  0    0   1
5       bm   0  1    0   0   0  0  0    0   0
6        l   1  1    1   1   1  1  1    0   1
7        m   1  0    1   0   0  0  1    1   1
8      sca   1  1    0   1   0  1  0    0   1
9      sia   0  1    1   1   1  1  0    0   1
10     tai   1  1    0   1   0  0  0    1   1
11      ua   1  1    1   1   0  0  1    1   1
12       v   1  1    0   1   0  1  1    0   1

【讨论】:

  • aa, ac, ana, .... 必须是列中的值,而不是索引。如示例所示。在示例列中的名称为“公司”
  • 已更新,请再查看
【解决方案2】:

让我们试试explode 然后crosstab

s = pd.Series(d['self']).apply(list).explode()
out = pd.crosstab(s.index,s).reindex(columns=d['attrs'],fill_value=0)
out =out.rename_axis(None).rename_axis(None,axis=1).reset_index().rename(columns={'index':'company'})
Out[193]: 
   company  LA  E  Can  AP  ME  A  M  Car  US
0       aa   0  0    0   1   0  0  0    0   0
1       ac   1  1    1   1   1  0  1    1   1
2      ana   0  1    0   1   0  0  0    0   1
3      anz   0  1    0   1   0  0  0    0   1
4       bm   0  1    0   0   0  0  0    0   0
5        l   1  1    1   1   1  1  1    0   1
6        m   1  0    1   0   0  0  1    1   1
7      sca   1  1    0   1   0  1  0    0   1
8      sia   0  1    1   1   1  1  0    0   1
9     taag   0  1    1   1   1  1  0    0   1
10     tai   1  1    0   1   0  0  0    1   1
11      ua   1  1    1   1   0  0  1    1   1
12       v   1  1    0   1   0  1  1    0   1

【讨论】:

  • 谢谢,什么是 row_0 ?它不应该在那里
  • 是否可以删除 row_0 并制作 col_0 列?所以我可以弄出 ["col_0"] 例如
  • @french_fries row_0 是索引名,col_0 是列名,不会影响任何事情
  • aa, ac, ana, .... 必须是列中的值,而不是索引。如示例所示。在示例列中的名称为“公司”
猜你喜欢
  • 2023-03-23
  • 2018-04-14
  • 2021-02-15
  • 2023-03-10
  • 2021-01-19
  • 2017-06-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多