如何使用Python将包含文本和范围的字典文件转换为Pandas数据框。

huangapple go评论56阅读模式
英文:

How to convert dictionary file with text and spans to pandas data frame using python

问题

I can help you translate the code-related text. Here's the translation:

我可以帮助您翻译与代码相关的文本。以下是翻译:

"I have a list which contains dictionary and list elements. The structure is similar to this:

我有一个包含字典和列表元素的列表。结构类似于这样:

[{'text': 'convert json 1 to dataframe', 'spans': [{'start': 0, 'end': 8, 'label': 'person', 'value': 'person1'}, {'start': 46, 'end': 56, 'label': 'dob', 'value': 'bday'}, {'start': 61, 'end': 71, 'label': 'location', 'value': 'US1'}]},

[{'text': 'convert json 2 to dataframe', 'spans': [{'start': 0, 'end': 8, 'label': 'person', 'value': 'person2'}, {'start': 10, 'end': 18, 'label': 'person', 'value': 'person3'}, {'start': 61, 'end': 71, 'label': 'location', 'value': 'US2'}]}, ]

它包括 textspans,其中 spans 是一个包含实体及其 startendlabelvalue 的列表。

How to convert it to Pandas Data frame in the below format:

如何将其转换为 Pandas 数据框以获取下面的格式:"

英文:

I have a list which contains dictionary and list elements. The structure is similar to this:

[{'text': 'convert json 1 to dataframe', 'spans': [{'start': 0, 'end': 8, 'label': 'person', 'value': 'person1'}, {'start': 46, 'end': 56, 'label': 'dob', 'value': 'bday'}, {'start': 61, 'end': 71, 'label': 'location', 'value': 'US1'}]}, 

{'text': 'convert json 2 to dataframe', 'spans': [{'start': 0, 'end': 8, 'label': 'person', 'value': 'person2'}, {'start': 10, 'end': 18, 'label': 'person', 'value': 'person3'}, {'start': 61, 'end': 71, 'label': 'location', 'value': 'US2'}]}, ] 

It has text and spans and spans is a list containing entities and their start, end, label, value

How to convert it to Pandas Data frame in the below format:

如何使用Python将包含文本和范围的字典文件转换为Pandas数据框。

答案1

得分: 2

使用json_normalize函数:

out = pd.json_normalize(d, record_path='spans', meta='text')

输出:

       start  end     label    value                         text
    0      0    8    person  person1  将JSON 1转换为数据帧
    1     46   56       dob     bday  将JSON 1转换为数据帧
    2     61   71  location      US1  将JSON 1转换为数据帧
    3      0    8    person  person2  将JSON 2转换为数据帧
    4     10   18    person  person3  将JSON 2转换为数据帧
    5     61   71  location      US2  将JSON 2转换为数据帧
英文:

with json_normalize:

out=pd.json_normalize(d, record_path='spans', meta='text')

Output:

   start  end     label    value                         text
0      0    8    person  person1  convert json 1 to dataframe
1     46   56       dob     bday  convert json 1 to dataframe
2     61   71  location      US1  convert json 1 to dataframe
3      0    8    person  person2  convert json 2 to dataframe
4     10   18    person  person3  convert json 2 to dataframe
5     61   71  location      US2  convert json 2 to dataframe

huangapple
  • 本文由 发表于 2023年3月23日 12:42:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75819318.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定