我有这样的数据结构:
data = [{
"name": "leopard",
"character": "mean",
"skills": ["sprinting", "hiding"],
"pattern": "striped",
},
{
"name": "antilope",
"character": "good",
"skills": ["running"],
},
.
.
.
]
字典中的每个键的值都为 integer , string 或 字符串列表(并非所有键都在所有字典中),每个 字典表示表中的一行;所有行均列为列表 字典。
如何将其轻松导入Pandas?我尝试过
df = pd.DataFrame.from_records(data)
但是在这里,我收到一个“ ValueError:数组必须全部长度相同”的错误。
答案 0 :(得分:0)
DataFrame
构造函数将基于行的数组(其他结构除外)作为数据输入。因此,以下工作原理:
data = [{
"name": "leopard",
"character": "mean",
"skills": ["sprinting", "hiding"],
"pattern": "striped",
},
{
"name": "antilope",
"character": "good",
"skills": ["running"],
}]
df = pd.DataFrame(data)
print(df)
输出:
character name pattern skills
0 mean leopard striped [sprinting, hiding]
1 good antilope NaN [running]