[英]Add new column based on values of another column from a dictionary in pandas
[英]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.