python - 如何将此 OrderedDict 排序为 pandas 数据框?
问题描述
我正在尝试将“epd:amount”中的这些 OrderedDicts 放入具有 3 列的 pandas Dataframe:'@xmlns:epd'; '@epd: 模块' ; '#文本'。我现在尝试了一段时间。有没有人猜的好?
OrderedDict([('epd:amount',
[OrderedDict([('@xmlns:epd',
'http://www.iai.kit.edu/EPD/2013'),
('@epd:module', 'A1'),
('#text', '0.0')]),
OrderedDict([('@xmlns:epd',
'http://www.iai.kit.edu/EPD/2013'),
('@epd:module', 'A2'),
('#text', '0.0')]),
OrderedDict([('@xmlns:epd',
'http://www.iai.kit.edu/EPD/2013'),
('@epd:module', 'A3'),
('#text', '0.927477418791169')]),
OrderedDict([('@xmlns:epd',
'http://www.iai.kit.edu/EPD/2013'),
('@epd:module', 'A1-A3'),
('#text', '0.927477418791169')]),
OrderedDict([('@xmlns:epd',
'http://www.iai.kit.edu/EPD/2013'),
('@epd:module', 'A5'),
('#text', '5.83315996933576')]),
OrderedDict([('@xmlns:epd',
'http://www.iai.kit.edu/EPD/2013'),
('@epd:module', 'C2'),
('#text', '0.0')]),
OrderedDict([('@xmlns:epd',
'http://www.iai.kit.edu/EPD/2013'),
('@epd:module', 'C3'),
('#text', '0.0')]),
OrderedDict([('@xmlns:epd',
'http://www.iai.kit.edu/EPD/2013'),
('@epd:module', 'D'),
('@epd:scenario', 'energetisch'),
('#text', '0.0')]),
OrderedDict([('@xmlns:epd',
'http://www.iai.kit.edu/EPD/2013'),
('@epd:module', 'D'),
('@epd:scenario', 'stofflich'),
('#text', '0.0')])])
解决方案
它是一个字典,因此您可以通过键获取一个值,然后将其转换为数据框。为简单起见,我删除了您的一些数据。
import pandas as pd
from collections import OrderedDict
x = OrderedDict([('epd:amount',
[OrderedDict([('@xmlns:epd',
'http://www.iai.kit.edu/EPD/2013'),
('@epd:module', 'A1'),
('#text', '0.0')]),
OrderedDict([('@xmlns:epd',
'http://www.iai.kit.edu/EPD/2013'),
('@epd:module', 'D'),
('@epd:scenario', 'stofflich'),
('#text', '0.0')])])])
df = pd.DataFrame(data['epd:amount'])
推荐阅读
- javascript - 反应本机相机不返回完整的文件路径
- node.js - 无法将 Angular 应用程序部署到 openshift:“参数错误,options.body。”
- powerapps - 显示模式下的复选框值 = 查看和编辑 PowerApps
- java - SonarLint Eclipse Extension 将包 src/company/projects/web... 中的 Java 文件视为测试
- matlab - 在应用程序设计器中提取绘制线坐标
- python - DecisionTreeRegression 提供 100% 的训练准确度,但在测试集中失败
- python - AWS Glue - Python Shell 作业 Secret Manager 连接问题
- cordova - 不能使用 ionic 和 form - 得到指令代理错误
- node.js - 从 MongoDB 返回 json
- python - 无法将字符串添加到 Python 中的字符串类型变量