将数组从JSON展平为pandas可以通过以下步骤实现:
import pandas as pd
import json
data = '''
[
{
"id": 1,
"name": "John",
"age": 30,
"skills": ["Python", "Java", "C++"]
},
{
"id": 2,
"name": "Jane",
"age": 25,
"skills": ["JavaScript", "HTML", "CSS"]
}
]
'''
json_data = json.loads(data)
df = pd.json_normalize(json_data)
print(df)
输出结果如下:
id name age skills
0 1 John 30 [Python, Java, C++]
1 2 Jane 25 [JavaScript, HTML, CSS]
通过以上步骤,我们成功将数组从JSON展平为pandas的DataFrame对象。在展平后的DataFrame中,每个数组元素都被展开为单独的行,并且相应的列被创建来存储数组的值。
领取专属 10元无门槛券
手把手带您无忧上云