如何在 Python 中将字典转换为数据框
How to convert dictionary to dataframe in Python
data = {'documents': [{'score': 0.8806856870651245, 'id': '1'}, {'score': 0.15902310609817505, 'id': '2'}, {'score': 0.9225043058395386, 'id': '3'}, {'score': 0.9872093200683594, 'id': '4'}], 'errors': []}
comments =
0 I love how we walk in to the fruit and vegetab...
1 When stores upgrade finished nothing to improve??
2 I was pleased with the cheerful efficiency wit...
3 Affordable prices, varieties and staff are ve..
有两部分数据。如何删除 data["errors"] 然后转换为如下所示的数据?在此合并评论数据后,即 Series?
score id comments
0.8806856870651245 1 I love how
0.15902310609817505 2 When stores
0.9225043058395386 3 I was pleased with
0.9872093200683594 4 Affordable prices
你不需要删除错误,你只需要通过访问数据中的 documents
创建数据框。此字典格式将自动转换为数据框,其中列是字典的键。
然后在首先通过 to_frame()
将评论转换为数据框后合并评论。请注意,我使用字符串值作为索引以匹配文档数据中的值。
# Create sample comments.
comments = pd.Series(['I love how', 'When stores', 'I was pleased with', 'Affordable prices'],
index=['1', '2', '3', '4'])
>>> pd.DataFrame(data['documents']).merge(
comments.to_frame('comments'), left_on='id', right_index=True)
id score comments
0 1 0.880686 I love how
1 2 0.159023 When stores
2 3 0.922504 I was pleased with
3 4 0.987209 Affordable prices
data = {'documents': [{'score': 0.8806856870651245, 'id': '1'}, {'score': 0.15902310609817505, 'id': '2'}, {'score': 0.9225043058395386, 'id': '3'}, {'score': 0.9872093200683594, 'id': '4'}], 'errors': []}
comments =
0 I love how we walk in to the fruit and vegetab...
1 When stores upgrade finished nothing to improve??
2 I was pleased with the cheerful efficiency wit...
3 Affordable prices, varieties and staff are ve..
有两部分数据。如何删除 data["errors"] 然后转换为如下所示的数据?在此合并评论数据后,即 Series?
score id comments
0.8806856870651245 1 I love how
0.15902310609817505 2 When stores
0.9225043058395386 3 I was pleased with
0.9872093200683594 4 Affordable prices
你不需要删除错误,你只需要通过访问数据中的 documents
创建数据框。此字典格式将自动转换为数据框,其中列是字典的键。
然后在首先通过 to_frame()
将评论转换为数据框后合并评论。请注意,我使用字符串值作为索引以匹配文档数据中的值。
# Create sample comments.
comments = pd.Series(['I love how', 'When stores', 'I was pleased with', 'Affordable prices'],
index=['1', '2', '3', '4'])
>>> pd.DataFrame(data['documents']).merge(
comments.to_frame('comments'), left_on='id', right_index=True)
id score comments
0 1 0.880686 I love how
1 2 0.159023 When stores
2 3 0.922504 I was pleased with
3 4 0.987209 Affordable prices