我有一个带有嵌入式列表的文档结构,在我使用聚合管道展开该结构之后,我最终得到以下内容
注意:我将Python与pymongo一起用于此查询
pipeline = [
{'$unwind': '$saved_alloys'},
{
'$project': {
'_id': 0,
'name': '$saved_alloys.name',
'compositions': '$saved_alloys.compositions'
}
}
]
res = db['alloys'].aggregate(pipeline)
for e in res:
print(e)
输出(截断):
{
'name': 'alloy-1',
'compositions': [
{'symbol': 'C', 'weight': 0.36},
{'symbol': 'Mn', 'weight': 1.41}
{'symbol': 'Si', 'weight': 1.03},
{'symbol': 'Ni', 'weight': 1.7}
]
}
{
'name': 'alloy-2',
'compositions': [
{'symbol': 'C', 'weight': 0.21},
{'symbol': 'Mn', 'weight': 0.23},
{'symbol': 'Si', 'weight': 0.86},
{'symbol': 'Ni', 'weight': 0.67},
{'symbol': 'Cr', 'weight': 0.12},
]
}
...
我试图通过将合成物的维数降低到笛卡尔平面来对此进行一些数据分析。因此,我想通过执行pd.DataFrame(list(res))
将其放入熊猫数据帧中
最后,我想给出一个具有以下结构的表:
{
"name": "alloy-1",
"C": 0.36,
"Mn": 1.41,
"Si": 1.03,
"Ni": 1.7,
"Cr": 0.0
},
{
"name": "alloy-2",
"C": 0.21,
"Mn": 0.23,
"Si": 0.86,
"Ni": 0.67,
"Cr": 0.12
}
注意compositions
列表可以有可变的大小,因此对于那些不在列表中的元素,我想添加它们,但是weights
值为零(如Cr
的alloy-1
)
提前感谢您的帮助
毫无疑问,这是可以优化的,但作为一种简单的入门方法,为每个返回的输出构造一个熊猫系列,并附加到数据帧中;最后用0.0替换任何“缺失”值
结果:
相关问题 更多 >
编程相关推荐