I am trying to convert a list of Pandas.Dataframe into a list of dictionnaries and trying to use the column "id" as a key for my dic. Not all of my dataframes have an id column, so I am checking before and some id columns are not. However, none of my results are using the id column as a key:
for dataframe in dataframes:
if 'id' in dataframe.columns:
cleaned_dic = dataframe.set_index('id').T.to_dict('dict')
else:
cleaned_dic = dataframe.T.to_dict('dict')
Example result in json
{
"0": {
"date": "2020-01-03",
"id": 9,
"journal": "Journal of photochemistry and photobiology. B, Biology",
"title": "Gold nanoparticles synthesized from Euphorbia fischeriana root by green route method alleviates the isoprenaline hydrochloride induced myocardial infarction in rats."
},
"1": {
"date": "01/01/2020",
"id": 10,
"journal": "The journal of maternal-fetal & neonatal medicine",
"title": "Clinical implications of umbilical artery Doppler changes after betamethasone administration"
},
"2": {
"date": "01/01/2020",
"id": "11",
"journal": "Journal of back and musculoskeletal rehabilitation",
"title": "Effects of Topical Application of Betamethasone on Imiquimod-induced Psoriasis-like Skin Inflammation in Mice."
},
"3": {
"date": "01/03/2020",
"id": "12",
"journal": "Journal of back and musculoskeletal rehabilitation",
"title": "Comparison of pressure release, phonophoresis and dry needling in treatment of latent myofascial trigger point of upper trapezius muscle."
}
}