Skip to content

Commit 0bbda54

Browse files
zzgaojowens
authored andcommitted
DOC: add example on json_normalize (pandas-dev#16438)
1 parent de60666 commit 0bbda54

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

doc/source/io.rst

+7
Original file line numberDiff line numberDiff line change
@@ -2020,6 +2020,13 @@ into a flat table.
20202020
.. ipython:: python
20212021
20222022
from pandas.io.json import json_normalize
2023+
data = [{'id': 1, 'name': {'first': 'Coleen', 'last': 'Volk'}},
2024+
{'name': {'given': 'Mose', 'family': 'Regner'}},
2025+
{'id': 2, 'name': 'Faye Raker'}]
2026+
json_normalize(data)
2027+
2028+
.. ipython:: python
2029+
20232030
data = [{'state': 'Florida',
20242031
'shortname': 'FL',
20252032
'info': {

pandas/io/json/normalize.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,16 @@ def json_normalize(data, record_path=None, meta=None,
135135
Examples
136136
--------
137137
138+
>>> from pandas.io.json import json_normalize
139+
>>> data = [{'id': 1, 'name': {'first': 'Coleen', 'last': 'Volk'}},
140+
... {'name': {'given': 'Mose', 'family': 'Regner'}},
141+
... {'id': 2, 'name': 'Faye Raker'}]
142+
>>> json_normalize(data)
143+
id name name.family name.first name.given name.last
144+
0 1.0 NaN NaN Coleen NaN Volk
145+
1 NaN NaN Regner NaN Mose NaN
146+
2 2.0 Faye Raker NaN NaN NaN NaN
147+
138148
>>> data = [{'state': 'Florida',
139149
... 'shortname': 'FL',
140150
... 'info': {
@@ -150,7 +160,6 @@ def json_normalize(data, record_path=None, meta=None,
150160
... },
151161
... 'counties': [{'name': 'Summit', 'population': 1234},
152162
... {'name': 'Cuyahoga', 'population': 1337}]}]
153-
>>> from pandas.io.json import json_normalize
154163
>>> result = json_normalize(data, 'counties', ['state', 'shortname',
155164
... ['info', 'governor']])
156165
>>> result

0 commit comments

Comments
 (0)