遍历嵌套字典

Loop through nested dictionary

我有这样的嵌套字典:

{
'location': {0: 'London', 1: 'London', 2: 'London', 3: 'London', 4: 'London', 5: 'London', 6: 'London'}, 
'attraction': {0: 'museum', 1: 'museum', 2: 'museum', 3: 'museum', 4: 'museum', 5: 'museum', 6: 'museum'},
'name': {0: 'London Museum', 1: 'British Museum', 2: 'Natural History Museum', 3: 'London's Docklands Museum', 4: 'Science Museum  ', 5: 'London Transport Museum ', 6: 'Victoria and Albert Museum'},
'rate': {0: 4.6, 1: 4.7, 2: 4.7, 3: 4.6, 4: 4.5, 5: 4.5, 6: 4.7}, 
'totalRates': {0: 13873, 1: 115863, 2: 13497, 3: 4528, 4: 51675, 5: 7349, 6: 42541},
'tag': {0: 'Muzeum', 1: 'Muzeum', 2: 'Muzeum', 3: 'Muzeum', 4: 'Muzeum', 5: 'Muzeum', 6: 'Muzeum'},
'address': {0: '150 London Wall', 1: 'Great Russell St', 2: 'Cromwell Rd', 3: '1 Warehouse. West India Quay. No. Hertsmere Rd', 4: 'Exhibition Rd', 5: 'Stare autobusy. pociągi i tramwaje Londynu', 6: 'Cromwell Rd'},
'description': {0: 'Some description', 1: 'Some description', 2: 'Some description', 3: 'Some description', 4: 'Some description', 5: 'Some description', 6: 'Some description'},
'image': {0: 'https://someImage.com/something', 1: 'https://someImage.com/something', 2: 'https://someImage.com/something', 3: 'https://someImage.com/something', 4: 'https://someImage.com/something', 5: 'https://someImage.com/something', 6: 'https://someImage.com/something'}
}

我想遍历并获取有关每个元素的信息,如下所示:

{
'location': 'London',
'attraction': 'museum',
'name': 'London Museum',
'rate': 4.6,
'totalRates': 13873,
'tag': 'Muzeum',
'address': '150 London Wall',
'description': 'Some description',
'image': 'https://someImage.com/something'
},
{
'location': 'London',
'attraction': 'museum',
'name': 'British Museum',
'rate': 4.7,
'totalRates': 115863,
'tag': 'Muzeum',
'address': 'Great Russell St',
'description': 'Some description',
'image': 'https://someImage.com/something'
},
ect...

我获得这个嵌套字典的方法是用 .to_dict() 方法转换 pandas.DataFrame

是否可以通过循环或以任何不同的方式转换数据帧来获得上述结果?我需要将结果作为字典,以便通过 django 上下文显示数据。

您需要在 to_dict() 中指定 orient=records:

>>> df.to_dict("records")

[{'location': 'London',
  'attraction': 'museum',
  'name': 'London Museum',
  'rate': 4.6,
  'totalRates': 13873,
  'tag': 'Muzeum',
  'address': '150 London Wall',
  'description': 'Some description',
  'image': 'https://someImage.com/something'},
 {'location': 'London',
  'attraction': 'museum',
  'name': 'British Museum',
  'rate': 4.7,
  'totalRates': 115863,
  'tag': 'Muzeum',
  'address': 'Great Russell St',
  'description': 'Some description',
  'image': 'https://someImage.com/something'},
 {'location': 'London',
  'attraction': 'museum',
  'name': 'Natural History Museum',
  'rate': 4.7,
  'totalRates': 13497,
  'tag': 'Muzeum',
  'address': 'Cromwell Rd',
  'description': 'Some description',
  'image': 'https://someImage.com/something'},
 {'location': 'London',
  'attraction': 'museum',
  'name': "London's Docklands Museum",
  'rate': 4.6,
  'totalRates': 4528,
  'tag': 'Muzeum',
  'address': '1 Warehouse. West India Quay. No. Hertsmere Rd',
  'description': 'Some description',
  'image': 'https://someImage.com/something'},
 {'location': 'London',
  'attraction': 'museum',
  'name': 'Science Museum  ',
  'rate': 4.5,
  'totalRates': 51675,
  'tag': 'Muzeum',
  'address': 'Exhibition Rd',
  'description': 'Some description',
  'image': 'https://someImage.com/something'},
 {'location': 'London',
  'attraction': 'museum',
  'name': 'London Transport Museum ',
  'rate': 4.5,
  'totalRates': 7349,
  'tag': 'Muzeum',
  'address': 'Stare autobusy. pociągi i tramwaje Londynu',
  'description': 'Some description',
  'image': 'https://someImage.com/something'},
 {'location': 'London',
  'attraction': 'museum',
  'name': 'Victoria and Albert Museum',
  'rate': 4.7,
  'totalRates': 42541,
  'tag': 'Muzeum',
  'address': 'Cromwell Rd',
  'description': 'Some description',
  'image': 'https://someImage.com/something'}]

假设您将数据保存在一个名为“字典”的变量中,这样的事情应该可行。

element_dicts = []
for i in range(len(dictionary['location'].keys())):
    data = dict()
    for each in range(len(dictionary.keys())):
         data[each] = dictionary[each][i]
    element_dicts.append(data)

"element_dicts" 现在将是一个列表,其中包含第 i 个索引处每个位置的字典。

nm = list(d.keys())
n = len(d[nm[0]])
[{k: d[k][i] for k in nm} for i in range(n)]