我有嵌套的字典,看起來像這樣:
{
'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...
我獲得此嵌套字典的方法是通過轉換pandas.DataFrame與.to_dict()方法。
是否可以通過以任何不同的方式回圈或轉換資料幀來獲得上述結果?我需要將結果作為字典,以便通過django背景關系顯示資料。
uj5u.com熱心網友回復:
您需要指定orient=records在to_dict():
>>> 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'}]
uj5u.com熱心網友回復:
假設你有一個名為“字典”的變數中的資料,這樣的事情應該可以作業。
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 個索引處每個位置的字典。
uj5u.com熱心網友回復:
nm = list(d.keys())
n = len(d[nm[0]])
[{k: d[k][i] for k in nm} for i in range(n)]
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/369910.html
標籤:Python 熊猫 数据框 字典 Django 上下文
