"name":"John",
"age":30,
"address":{
"city":"New York",
"zip":"10001"
}
}
# 嵌套的 JSON 数据
data = {
"name": "John",
"age": 30,
"address": {
"city": "New York",
"zip": "10001"
}
}
# 使用 json_normalize 规范化
df = pd.json_normalize(data)
# 打印 DataFrame
print(df)
"name":"John",
"age":30,
"skills":[
{"language":"Python", "level":"Intermediate"},
{"language":"JavaScript", "level":"Advanced"}
]
}
data_with_array = {
"name": "John",
"age": 30,
"skills": [
{"language": "Python", "level": "Intermediate"},
{"language": "JavaScript", "level": "Advanced"}
]
}
# 使用 json_normalize 规范化,指定嵌套数组路径
df_with_array = pd.json_normalize(data_with_array, record_path='skills')
# 打印 DataFrame
print(df_with_array)
"name":"John",
"age":30,
"contact":{
"email":"john@example.com",
"phone":{
"home":"123-456-7890",
"work":"987-654-3210"
}
}
}
data_nested = {
"name": "John",
"age": 30,
"contact": {
"email": "john@example.com",
"phone": {
"home": "123-456-7890",
"work": "987-654-3210"
}
}
}
# 使用 json_normalize 规范化,指定嵌套层次分隔符
df_nested = pd.json_normalize(data_nested, sep='_')
# 打印 DataFrame
print(df_nested)
了解更多数据分析知识、与更多优秀的人一起进群交流请扫码


