如何将分层字典转换为pandas Dataframe

taor4pac  于 2023-05-12  发布在  其他
关注(0)|答案(2)|浏览(101)

我想得到一个分层的数据

delhi = 10 vehicle
1 vehicle = 1 battery
1 battery = 4 packs
 a pack = 2 modules 
1 module = 104 cells

在pandas Dataframe 中
下面是一个层次结构字典的例子,我想把它转换成一个 Dataframe ,如图所示
[![我想从字典中转换的 Dataframe ][1]][1]

tempdict = {
    'vehicle1':[
        {'pack1':[{'module1':pack104}, {'module2':pack104}]}, 
        {'pack2':[{'module1':pack104}, {'module2':pack104}]}, 
        {'pack3':[{'module1':pack104}, {'module2':pack104}]}, 
        {'pack4':[{'module1':pack104}, {'module2':pack104}]}
    ]
}

这是我想转换的整本字典

listone = ['pack' ]
listtwo = ["{0}".format(i) for i in range(1,105)]
pack104 = list(''.join(e) for e in itertools.product(listone, listtwo))
dict = {'delhi': [{'vehicle1':[{'pack1':[{'module1':pack104}, {'module2':pack104}]}, {'pack2':[{'module1':pack104}, {'module2':pack104}]}, {'pack3':[{'module1':pack104}, {'module2':pack104}]}, {'pack4':[{'module1':pack104}, {'module2':pack104}]}]},
 {'vehicle2':[{'pack1':[{'module1':pack104}, {'module2':pack104}]}, {'pack2':[{'module1':pack104}, {'module2':pack104}]}, {'pack3':[{'module1':pack104}, {'module2':pack104}]}, {'pack4':[{'module1':pack104}, {'module2':pack104}]}]},
 {'vehicle3':[{'pack1':[{'module1':pack104}, {'module2':pack104}]},{ 'pack2':[{'module1':pack104}, {'module2':pack104}]}, {'pack3':[{'module1':pack104}, {'module2':pack104}]}, {'pack4':[{'module1':pack104}, {'module2':pack104}]}]},
 {'vehicle4':[{'pack1':[{'module1':pack104}, {'module2':pack104}]}, {'pack2':[{'module1':pack104}, {'module2':pack104}]}, {'pack3':[{'module1':pack104}, {'module2':pack104}]}, {'pack4':[{'module1':pack104}, {'module2':pack104}]}]},
 {'vehicle5':[{'pack1':[{'module1':pack104}, {'module2':pack104}]}, {'pack2':[{'module1':pack104}, {'module2':pack104}]}, {'pack3':[{'module1':pack104}, {'module2':pack104}]}, {'pack4':[{'module1':pack104}, {'module2':pack104}]}]},
 {'vehicle6':[{'pack1':[{'module1':pack104}, {'module2':pack104}]}, {'pack2':[{'module1':pack104}, {'module2':pack104}]}, {'pack3':[{'module1':pack104}, {'module2':pack104}]}, {'pack4':[{'module1':pack104}, {'module2':pack104}]}]},
 {'vehicle7':[{'pack1':[{'module1':pack104}, {'module2':pack104}]}, {'pack2':[{'module1':pack104}, {'module2':pack104}]}, {'pack3':[{'module1':pack104}, {'module2':pack104}]}, {'pack4':[{'module1':pack104}, {'module2':pack104}]}]},
 {'vehicle8':[{'pack1':[{'module1':pack104}, {'module2':pack104}]}, {'pack2':[{'module1':pack104}, {'module2':pack104}]}, {'pack3':[{'module1':pack104}, {'module2':pack104}]}, {'pack4':[{'module1':pack104}, {'module2':pack104}]}]},
 {'vehicle9':[{'pack1':[{'module1':pack104}, {'module2':pack104}]}, {'pack2':[{'module1':pack104}, {'module2':pack104}]}, {'pack3':[{'module1':pack104}, {'module2':pack104}]}, {'pack4':[{'module1':pack104}, {'module2':pack104}]}]},
 {'vehicle10':[{'pack1':[{'module1':pack104}, {'module2':pack104}]}, {'pack2':[{'module1':pack104}, {'module2':pack104}]}, {'pack3':[{'module1':pack104}, {'module2':pack104}]}, {'pack4':[{'module1':pack104}, {'module2':pack104}]}]}]}

'''



[1]: https://i.stack.imgur.com/5mcoJ.png
qltillow

qltillow1#

您可以将分层字典转换为Pandas dataframe:
1.导入Pandas库。
1.创建一个dictionary解析来创建一个dictionary列表,其中每个dictionary代表dataframe中的一行。
1.使用DataFrame()构造函数从字典列表中创建数据框。
1.将数据框的索引设置为
vehicle
列,显示数据框。
你的py代码:

import pandas as pd

# Create a dictionary comprehension to create a list of dictionaries
df_list = [{
   'vehicle': key,
   'battery': value['battery'],
   'packs': value['packs'],
   'modules': value['modules'],
   'cells': value['cells']
  } for key, value in tempdict.items()]

# Create a dataframe from the list of dictionaries
df = pd.DataFrame(df_list)

# Set the index of the dataframe to the `vehicle` column
df.set_index('vehicle', inplace=True)

# Display the dataframe
df

输出:

Vehicle  battery  packs  modules  cells
0  vehicle1     1       4       2      104
1  vehicle2     1       4       2      104
2  vehicle3     1       4       2      104
3  vehicle4     1       4       2      104
jtw3ybtb

jtw3ybtb2#

你可以使用循环和DataFrame构造函数:

df = pd.DataFrame([{'vehicle': k, 'pack': k2, 'module': next(iter(d2))}
                   for k, l in tempdict.items() for d in l
                   for k2, l2 in d.items() for d2 in l2])

输出:

vehicle   pack   module
0  vehicle1  pack1  module1
1  vehicle1  pack1  module2
2  vehicle1  pack2  module1
3  vehicle1  pack2  module2
4  vehicle1  pack3  module1
5  vehicle1  pack3  module2
6  vehicle1  pack4  module1
7  vehicle1  pack4  module2

相关问题