问题描述
我试图从字典列表中制作一个数据框。我对整个编程事物都很陌生,而谷歌只会让我更加困惑。这就是为什么我转向你们希望得到一些帮助。 前两个列表值 (YV01','3nP3RFgGnbrofILK4DF2Tp) 我想要在名为:Name 和 GlobalId 的列下。我会撒谎放弃 Pset_wallcommon、AC_Pset_RenovationAndPhasing 和 BaseQuantities。并使用其余的键(如果它们被称为)作为列名。
如果有人能给我正确的推动,那就太好了:)
记录:我正在使用 Ifcopenshell 包解析 Ifc 文件
数据:
['YV01','3nP3RFgGnbrofILK4DF2Tp',{'Pset_WallCommon': {'Combustible': False,'Compartmentation': False,'ExtendToStructure': False,'SurfaceSpreadOfFlame': '','Transference:0'Thermal:'Thermal '','Acousticrating': '','Firerating': '','Loadbearing': False,'IsExternal': False},'AC_Pset_RenovationAndPhasing': {'Renovation Status': 'New'},'BaseQuantities': { '长度':13786.7314346, '身高':2700.0, '宽度':276.0, 'GrossFootprintArea':3.88131387595, 'NetFootprintArea':3.88131387595, 'GrossSideArea':37.9693748734, 'NetSideArea':37.9693748734, 'GrossVolume':10.4795474651,“NetVolume ': 10.4795474651}},'YV01','1M4JyBJhXD5xt8fBFUcjUU','ExtendToface:ThermofSurf','Transmitance':,'Reference': '','基本数量':{'Len第g':6166.67382573, '身高':2700.0, '宽度':276.0, 'GrossFootprintArea':1.6258259759, 'NetFootprintArea':1.6258259759, 'GrossSideArea':15.9048193295, 'NetSideArea':15.9048193295, 'GrossVolume':4.38973013494, 'NetVolume' :4.38973013494}}
all_walls = ifc_file.by_type('IfcWall')
wallList = []
for wall in all_walls:
propertySets = (ifcopenshell.util.element.get_psets(wall))
wallList.append(wall.Name)
wallList.append(wall.GlobalId)
wallList.append(propertySets)
print(wallList)
wall_table = pd.DataFrame.from_records(wallList)
print(wall_table)
我已经尝试过这些基本的 pd.DataFrame.from_dict/records/arrays(data) 但输出看起来像这样
更新:非常感谢您的帮助,我从中学到了很多! 所以我从 wallList 中制作了一本字典,并将字典压平。像这样:
#list of walls
for wall in all_walls:
propertySets = (ifcopenshell.util.element.get_psets(wall))
wallList.append(wall.Name)
wallList.append(wall.GlobalId)
wallList.append(propertySets)
#dict from list
wall_dict = {i: wallList[i] for i in range(0,len(wallList))}
new_dict = {}
#flattening dict
for key,value in wall_dict.items():
if isinstance(value,dict):
for key in value.keys():
for key2 in value[key].keys():
new_dict[key + '_' + key2] = value[key][key2]
else:
new_dict[key] = value
wall_table = pd.DataFrame.from_dict(new_dict,orient='index')
print(wall_table)
它似乎工作得很好,唯一的问题是数据框包含所有墙壁,但仅包含列表中第一个的属性集数据。我似乎无法理解 dict 展平循环是如何工作的。我还希望索引名称(Pset_WallCommon_Combustible 等)成为我的数据框中的列。这可能吗?
解决方法
您可以更改代码以添加 Name
和 GlobalID
作为 propertySets
的值,然后执行 pd.DataFrame.from_records()
all_walls = ifc_file.by_type('IfcWall')
wallList = []
for wall in all_walls:
propertySets = (ifcopenshell.util.element.get_psets(wall))
propertySets['Name'] = wall.Name
PropertySets['GlobalId'] = wall.GlobalId
wallList.append(propertySets)
wall_table = pd.DataFrame.from_records(wallList)
,
编辑:像我一样简单地扁平化列表无济于事。实际上,我认为您应该完全放弃这个列表,并尝试从字典中加载 Dataframe。我们需要看看 all_walls
是什么样子来帮助您,不过。
您是否尝试过将 all_walls
字典直接加载到数据框:df = pd.Dataframe.from_dict(all_walls)
中?
我认为如果这不起作用,以类似于以下的方式扁平化字典应该可以解决问题。
new_dict = {}
for key,value in all_walls.items():
if isinstance(value,dict):
for key in value.keys():
for key2 in value[key].keys():
new_dict[key + '_' + key2] = value[key][key2]
else:
new_dict[key] = value