例如,我有一個數據框df = {'dicts':[{'id': 0, 'text': 'Willamette'},{'id': 1, 'text': 'Valley'}], 'ner': ["Person", "Location"]}df= pd.DataFrame(df)` 我想要像這樣的最終結果{'id': 0, 'text': 'Willamette', 'ner': 'Person'}{'id': 1, 'text': 'Valley', 'ner': 'Location'}`我正在使用以下邏輯,但它對我不起作用-for i, rows in df["dicts"].iteritems(): for cat in df['ner']: df["dicts"][i]=df["dicts"][i].update({'ner' : df['ner'][cat]})我該如何解決這個問題?
1 回答

叮當貓咪
TA貢獻1776條經驗 獲得超12個贊
IIUC
d=pd.DataFrame(df.dicts.tolist(),index=df.index).join(df[['ner']]).to_dict('r')[{'id': 0, 'text': 'Willamette', 'ner': 'Person'}, {'id': 1, 'text': 'Valley', 'ner': 'Location'}]
添加回答
舉報
0/150
提交
取消