【发布时间】:2021-08-09 23:48:53
【问题描述】:
我正在处理一个大型 JSON 文件,特别是角色数据集 (download here)
Persona-Chat 中的每个条目都是一个具有两个键个性和 话语,数据集是条目列表。
personality: list of strings containing the personality of the agent utterances: list of dictionaries, each of which has two keys which are lists of strings. candidates: [next_utterance_candidate_1, ..., next_utterance_candidate_19] The last candidate is the ground truth response observed in the conversational data history: [dialog_turn_0, ... dialog_turn N], where N is an odd number since the other user starts every conversation.https://towardsdatascience.com/how-to-train-your-chatbot-with-simple-transformers-da25160859f4
我想要实现的是将其展平并以以下格式将其转换为 tsv:
col_index, string (where string is the personality, candidates and history
但是 每当我尝试加载它并将其转换为数据帧时
import pandas as pd
df = pd.read_json(r'path')
display(df)
我收到以下错误:
ValueError: arrays must all be same length
感谢任何帮助,无论是文章还是其他库/框架和方法,甚至是面包屑!
编辑: 我正在将它提供给另一个需要 tsv 的 api,我正在考虑一种连接并保留结构以再次重新构建它的方法。
【问题讨论】:
-
不要使用
pd.read_json()尝试读取非表格数据。只需使用json.load()。 -
另外,考虑到
personality/candidates/history中的条目数量可变,你的 CSV 文件会很奇怪。 -
我正在将它提供给另一个需要 tsv 的 api,我正在考虑一种连接并保留结构以再次重新构建它的方法。感谢 json.load() 评论。
标签: python json pandas dataframe