Normalize JSON data into a flat table.
data=[{'id':1,
'name':{'first':'Coleen', 'last':'Volk'}},
{'name':{'given':'Mose','family':'Regner'}},
{'id':2,
'name':'Faye Raker'}]
pd.json_normalize(data)
>>>
id name.first name.last name.given name.family name
0 1.0 Coleen Volk NaN NaN NaN
1 NaN NaN NaN Mose Regner NaN
2 2.0 NaN NaN NaN NaN Faye Raker
data=[{'state':'florida',
'shortname':'FL',
'info':{'governor':'rick scott'},
'countries':[{'name':'dade','population':12345},
{'name':'broward','population':40000},
{'name':'palm beach','population':60000}]},
{'state':'ohio',
'shortname':'OH',
'info':{'governor':'john'},
'countries':[{'name':'summit','population':1234},
{'name':'cuya','population':1333}]}]
result=pd.json_normalize(data,'countries',['state','shortname',
['info','governor']])
result
>>>
name population state shortname info.governor
0 dade 12345 florida FL rick scott
1 broward 40000 florida FL rick scott
2 palm beach 60000 florida FL rick scott
3 summit 1234 ohio OH john
4 cuya 1333 ohio OH john
'Analyze Data > Python Libraries' 카테고리의 다른 글
regular expression (0) | 2022.04.26 |
---|---|
collections-Counter, most_common, FreqDist, defaultdict (0) | 2022.03.04 |
mlxtend-TransactionEncoder, association_rules (0) | 2021.06.23 |
pandas-4. read_csv, unique, to_csv, file upload, file download (0) | 2021.06.22 |
numpy-array, arange, reshape, slicing, newaxis, ...(Ellipsis) (0) | 2021.05.25 |