如何将词典列表合并到单个词典中?

2024-10-02 22:23:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个嵌套字典,其中包含一个字典列表。我怎么把它们分开?你知道吗

输入

df=pd.DataFrame({'Hashtags' : [{u'indices': [53, 65], u'text': u'Predictions'}, {u'indices': [67, 76], u'text': u'FreeTips'}, {u'indices': [78, 89], u'text': u'SoccerTips'}, {u'indices': [90, 103], u'text': u'FootballTips'}, {u'indices': [104, 110], u'text': u'Goals'}]})

预期产量:

{'Hashtags' :["u'Predictions'", "u'SoccerTips'", "u'FootballTips'", "u'Goals'"]}

Tags: textdataframedf列表字典pd产量goals
1条回答
网友
1楼 · 发布于 2024-10-02 22:23:34
df = pd.DataFrame({'Hashtags' : [{u'indices': [53, 65], u'text': u'Predictions'}, {u'indices': [67, 76], u'text': u'FreeTips'}, {u'indices': [78, 89], u'text': u'SoccerTips'}, {u'indices': [90, 103], u'text': u'FootballTips'}, {u'indices': [104, 110], u'text': u'Goals'}]})

texts = [x['text'] for x in df.values.flatten()]

output = {'Hashtags': texts }

相关问题 更多 >