插入我的嵌套数据框到我的数据库
问题描述:
我有一个数据框,其中嵌套字典项目,我不能存储该数据框到我的数据库。它给了我一个错误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': '[email protected]', '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是我的字典里,我的数据帧的输出是这样的:
OUTPUT:
id 10153836054533219
birthday 04/17/1993
education [{'school': {'id': '281278052799', 'name': 'Co...
email [email protected]
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
答
如果你想要的方式来呈现这使得你可以写这一个数据库...使用json
import json
df.applymap(json.dumps)
0
id "10153836054533219"
birthday "04/17/1993"
education [{"school": {"id": "281278052799", "name": "Co...
email "[email protected]"
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
字符串。
我不想在我的数据库中抛出一个转储文件。我希望它是结构化的,例如我的数据库中的列是id和value。我希望密钥存储在id中,并将值存储在value列中。 –