簡體   English   中英

如何將 pandas dataframe 中的列值作為新的鍵值對添加到具有字典的另一列中?

[英]How can I add column values from a pandas dataframe as a new key value pair in another column having dictionary?

例如,我有一個 dataframe

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]})

我該如何解決這個問題?

國際大學聯盟

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'}]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM