[英]How to create a Pandas DataFrame from row-based list of dictionaries
I have a data structure like this: 我有这样的数据结构:
data = [{
"name": "leopard",
"character": "mean",
"skills": ["sprinting", "hiding"],
"pattern": "striped",
},
{
"name": "antilope",
"character": "good",
"skills": ["running"],
},
.
.
.
]
Each key in the dictionaries has values of type integer , string or list of strings (not all keys are in all dicts present), each dictionary represents a row in a table; 字典中的每个键都具有整数 , 字符串或字符串 列表类型的值(并非所有键都存在于所有字典中),每个词典表示表中的一行; all rows are given as the list of dictionaries.
所有行均作为字典列表给出。
How can I easily import this into Pandas? 如何将其轻松导入Pandas? I tried
我试过了
df = pd.DataFrame.from_records(data)
but here I get an "ValueError: arrays must all be same length" error. 但是在这里,我收到一个“ ValueError:数组必须全部长度相同”的错误。
The DataFrame
constructor takes row-based arrays (amoungst other structures) as data input. DataFrame
构造函数将基于行的数组(以及其他结构)作为数据输入。 Therefore the following works: 因此,以下工作原理:
data = [{
"name": "leopard",
"character": "mean",
"skills": ["sprinting", "hiding"],
"pattern": "striped",
},
{
"name": "antilope",
"character": "good",
"skills": ["running"],
}]
df = pd.DataFrame(data)
print(df)
Output: 输出:
character name pattern skills
0 mean leopard striped [sprinting, hiding]
1 good antilope NaN [running]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.