问题
I have a JSON file like below,
{
“A”:1,
“B”:2,
“C”: [
{“x”:1,“y”:2,“z”:3},
{"x":2,"y":7,"z":77}
]
}
pandas.from_json returns me data frame with column A,B and C. But, actually I am looking for columns with x,y and z. Is there a way to get it?
回答1:
You can use json_normalize:
json = {
"A":1,
"B":2,
"C": [{"x":1,"y":2,"z":3 },
{"x":2,"y":7,"z":77}]
}
from pandas.io.json import json_normalize
df = json_normalize(json, 'C')
print (df)
x y z
0 1 2 3
1 2 7 77
If need all columns:
df = json_normalize(json, 'C', ['A','B'])
print (df)
x y z B A
0 1 2 3 2 1
1 2 7 77 2 1
来源:https://stackoverflow.com/questions/40100106/pandas-from-json-method-usage