我有一个数据框,其中包含嵌套的字典项,我无法将该数据框存储到我的数据库中。它给我一个TypeError: sequence item 0: expected str instance, dict found
的错误。有没有解决这个问题的方法
CODE:
df = pd.DataFrame.from_dict(mprofile,orient='index')
我的字典mprofile的输出是这样的:
{'id': '10153836054533219', 'birthday': '04/17/1993', 'education': [{'school': {'id': '281278052799', 'name': 'Comsats Islamabad'}, 'type': 'High School', 'id': '10152109501113219'}, {'school': {'id': '114889221857515', 'name': 'IMCB G-10/4'}, 'type': 'High School', 'id': '10150528781298219'}, {'school': {'id': '281278052799', 'name': 'Comsats Islamabad'}, 'type': 'College', 'id': '10152183540228219'}], 'email': 'mj_90tr@hotmail.com', 'favorite_athletes': [{'id': '167866666571743', 'name': 'Luis Suarez'}, {'id': '169875889388', 'name': 'Philipp Lahm'}, {'id': '226625490712161', 'name': 'David Luiz'}, {'id': '79298470625', 'name': 'Andrés Iniesta'}, {'id': '511845538832537', 'name': 'Gareth Bale'}, {'id': '142523572511686', 'name': 'Ronaldinho Gaúcho'}, {'id': '176063032413299', 'name': 'Leo Messi'}, {'id': '249113725102662', 'name': 'Thomas Müller'}, {'id': '185923878109545', 'name': 'Mario Götze'}, {'id': '124037987606288', 'name': 'Mats Hummels'}, {'id': '7991759722', 'name': 'Bastian Schweinsteiger'}, {'id': '9752007795', 'name': 'Shawn Michaels'}, {'id': '148456285190063', 'name': 'Neymar Jr.'}, {'id': '552825254796051', 'name': 'Mesut Özil'}, {'id': '7550279682', 'name': 'Paul "Triple H" Levesque'}, {'id': '185950250927', 'name': 'Brock Lesnar'}, {'id': '14917350275', 'name': 'Jay Cutler'}, {'id': '169376299783862', 'name': 'Roger Federer Just peRFect'}, {'id': '417539191628925', 'name': 'Abdul Razzaq'}, {'id': '29756652821', 'name': 'Muhammad Ali'}, {'id': '606014986100673', 'name': 'Shoaib Akhtar'}, {'id': '145425262156250', 'name': 'Sheheryar-pro freestyler'}, {'id': '245323855526590', 'name': 'Wayne Rooney'}, {'id': '105587166141910', 'name': 'Shoaib Akhtar'}], 'favorite_teams': [{'id': '663371713769515', 'name': "Pakistan's Got Talent"}, {'id': '146078378744600', 'name': 'ICC - International Cricket Council'}, {'id': '599005550129062', 'name': 'Isb kpk undergrounds'}, {'id': '168868979824907', 'name': 'Drifting - Tuning'}, {'id': '322125507869686', 'name': 'KPK Underground'}, {'id': '269578509818086', 'name': 'The Bullz'}, {'id': '193575534016977', 'name': 'Twin City Freestyle Society'}, {'id': '365160980224559', 'name': 'Racing Community of Pakistan (RCOP)'}, {'id': '143806589018889', 'name': 'Pakistan Cricket Fans'}], 'first_name': 'Faizan', 'gender': 'male', 'hometown': {'id': '111501225536407', 'name': 'Islamabad, Pakistan'}, 'last_name': 'Ahmad', 'link': 'https://www.facebook.com/app_scoped_user_id/10153836054533219/', 'location': {'id': '111501225536407', 'name': 'Islamabad, Pakistan'}, 'locale': 'en_US', 'name': 'Faizan Ahmad', 'timezone': 5, 'updated_time': '2017-03-20T19:32:12+0000', 'verified': True}
mprofile是我的字典,我的数据框的输出是这样的:
输出:
id 10153836054533219
birthday 04/17/1993
education [{'school': {'id': '281278052799', 'name': 'Co...
email mj_90tr@hotmail.com
favorite_athletes [{'id': '167866666571743', 'name': 'Luis Suare...
favorite_teams [{'id': '663371713769515', 'name': 'Pakistan's...
first_name Faizan
gender male
hometown {'id': '111501225536407', 'name': 'Islamabad, ...
last_name Ahmad
link https://www.facebook.com/app_scoped_user_id/10...
location {'id': '111501225536407', 'name': 'Islamabad, ...
locale en_US
name Faizan Ahmad
timezone 5
updated_time 2017-03-20T19:32:12+0000
verified True
答案 0 :(得分:0)
如果您想要一种方式来呈现这一点,您可以将其写入数据库...使用json
import json
df.applymap(json.dumps)
0
id "10153836054533219"
birthday "04/17/1993"
education [{"school": {"id": "281278052799", "name": "Co...
email "mj_90tr@hotmail.com"
favorite_athletes [{"id": "167866666571743", "name": "Luis Suare...
favorite_teams [{"id": "663371713769515", "name": "Pakistan's...
first_name "Faizan"
gender "male"
hometown {"id": "111501225536407", "name": "Islamabad, ...
last_name "Ahmad"
link "https://www.facebook.com/app_scoped_user_id/1...
location {"id": "111501225536407", "name": "Islamabad, ...
locale "en_US"
name "Faizan Ahmad"
timezone 5
updated_time "2017-03-20T19:32:12+0000"
verified true
这些现在都是json
个字符串。