首页 > 解决方案 > 如何将此 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')])])

标签: pythonxmldictionaryordereddictionary

解决方案


它是一个字典,因此您可以通过键获取一个值,然后将其转换为数据框。为简单起见,我删除了您的一些数据。

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'])


推荐阅读