Pyhton 将json转换为dataframe
一.内容
json:
{"FirstName":"John",
"LastName":"Mark",
"MiddleName":"Lewis",
"username":"johnlewis2",
"password":"2910"}
dataframe
Column -> FirstName | LastName | MiddleName | username | password
Row -----> John | Mark |Lewis | johnlewis2 |2910
Python code
import pandas as pd
data = [{'name': 'vikash', 'age': 27}, {'name': 'Satyam', 'age': 14}]
df = pd.DataFrame.from_dict(data, orient='columns')
df
Out[4]:
age name
0 27 vikash
1 14 Satyam
from pandas.io.json import json_normalize
data = [
{
'name': {
'first': 'vikash',
'last': 'singh'
},
'age': 27
},
{
'name': {
'first': 'satyam',
'last': 'singh'
},
'age': 14
}
]
df = pd.DataFrame.from_dict(json_normalize(data), orient='columns')
df
Out[8]:
age name.first name.last
0 27 vikash singh
1 14 satyam singh