【发布时间】:2026-01-11 18:25:01
【问题描述】:
我正在尝试将 Pandas.Dataframe 列表转换为字典列表,并尝试使用“id”列作为我的 dic 的键。并非我所有的数据框都有id 列,所以我之前检查过,有些 id 列没有。但是,我的结果都没有使用 id 列作为键:
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')
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."
}
}
【问题讨论】:
-
您的代码和数据与问题的标题无关。请添加您遇到的错误的代码和堆栈跟踪或更正标题。