@@ -70,15 +70,17 @@ def nested_to_record(
70
70
71
71
Examples
72
72
--------
73
- IN[52]: nested_to_record(dict(flat1=1,dict1=dict(c=1,d=2),
74
- nested=dict(e=dict(c=1,d=2),d=2)))
75
- Out[52]:
76
- {'dict1.c': 1,
77
- 'dict1.d': 2,
78
- 'flat1': 1,
79
- 'nested.d': 2,
80
- 'nested.e.c': 1,
81
- 'nested.e.d': 2}
73
+ >>> nested_to_record(
74
+ ... dict(flat1=1, dict1=dict(c=1, d=2), nested=dict(e=dict(c=1, d=2), d=2))
75
+ ... )
76
+ {\
77
+ 'flat1': 1, \
78
+ 'dict1.c': 1, \
79
+ 'dict1.d': 2, \
80
+ 'nested.e.c': 1, \
81
+ 'nested.e.d': 2, \
82
+ 'nested.d': 2\
83
+ }
82
84
"""
83
85
singleton = False
84
86
if isinstance (ds , dict ):
@@ -208,18 +210,21 @@ def _simple_json_normalize(
208
210
209
211
Examples
210
212
--------
211
- IN[52]: _simple_json_normalize({
212
- 'flat1': 1,
213
- 'dict1': {'c': 1, 'd': 2},
214
- 'nested': {'e': {'c': 1, 'd': 2}, 'd': 2}
215
- })
216
- Out[52]:
217
- {'dict1.c': 1,
218
- 'dict1.d': 2,
219
- 'flat1': 1,
220
- 'nested.d': 2,
221
- 'nested.e.c': 1,
222
- 'nested.e.d': 2}
213
+ >>> _simple_json_normalize(
214
+ ... {
215
+ ... "flat1": 1,
216
+ ... "dict1": {"c": 1, "d": 2},
217
+ ... "nested": {"e": {"c": 1, "d": 2}, "d": 2},
218
+ ... }
219
+ ... )
220
+ {\
221
+ 'flat1': 1, \
222
+ 'dict1.c': 1, \
223
+ 'dict1.d': 2, \
224
+ 'nested.e.c': 1, \
225
+ 'nested.e.d': 2, \
226
+ 'nested.d': 2\
227
+ }
223
228
224
229
"""
225
230
normalised_json_object = {}
@@ -283,22 +288,30 @@ def _json_normalize(
283
288
284
289
Examples
285
290
--------
286
- >>> data = [{'id': 1, 'name': {'first': 'Coleen', 'last': 'Volk'}},
287
- ... {'name': {'given': 'Mark', 'family': 'Regner'}},
288
- ... {'id': 2, 'name': 'Faye Raker'}]
291
+ >>> data = [
292
+ ... {"id": 1, "name": {"first": "Coleen", "last": "Volk"}},
293
+ ... {"name": {"given": "Mark", "family": "Regner"}},
294
+ ... {"id": 2, "name": "Faye Raker"},
295
+ ... ]
289
296
>>> pd.json_normalize(data)
290
297
id name.first name.last name.given name.family name
291
298
0 1.0 Coleen Volk NaN NaN NaN
292
299
1 NaN NaN NaN Mark Regner NaN
293
300
2 2.0 NaN NaN NaN NaN Faye Raker
294
301
295
- >>> data = [{'id': 1,
296
- ... 'name': "Cole Volk",
297
- ... 'fitness': {'height': 130, 'weight': 60}},
298
- ... {'name': "Mark Reg",
299
- ... 'fitness': {'height': 130, 'weight': 60}},
300
- ... {'id': 2, 'name': 'Faye Raker',
301
- ... 'fitness': {'height': 130, 'weight': 60}}]
302
+ >>> data = [
303
+ ... {
304
+ ... "id": 1,
305
+ ... "name": "Cole Volk",
306
+ ... "fitness": {"height": 130, "weight": 60},
307
+ ... },
308
+ ... {"name": "Mark Reg", "fitness": {"height": 130, "weight": 60}},
309
+ ... {
310
+ ... "id": 2,
311
+ ... "name": "Faye Raker",
312
+ ... "fitness": {"height": 130, "weight": 60},
313
+ ... },
314
+ ... ]
302
315
>>> pd.json_normalize(data, max_level=0)
303
316
id name fitness
304
317
0 1.0 Cole Volk {'height': 130, 'weight': 60}
@@ -307,32 +320,49 @@ def _json_normalize(
307
320
308
321
Normalizes nested data up to level 1.
309
322
310
- >>> data = [{'id': 1,
311
- ... 'name': "Cole Volk",
312
- ... 'fitness': {'height': 130, 'weight': 60}},
313
- ... {'name': "Mark Reg",
314
- ... 'fitness': {'height': 130, 'weight': 60}},
315
- ... {'id': 2, 'name': 'Faye Raker',
316
- ... 'fitness': {'height': 130, 'weight': 60}}]
323
+ >>> data = [
324
+ ... {
325
+ ... "id": 1,
326
+ ... "name": "Cole Volk",
327
+ ... "fitness": {"height": 130, "weight": 60},
328
+ ... },
329
+ ... {"name": "Mark Reg", "fitness": {"height": 130, "weight": 60}},
330
+ ... {
331
+ ... "id": 2,
332
+ ... "name": "Faye Raker",
333
+ ... "fitness": {"height": 130, "weight": 60},
334
+ ... },
335
+ ... ]
317
336
>>> pd.json_normalize(data, max_level=1)
318
337
id name fitness.height fitness.weight
319
338
0 1.0 Cole Volk 130 60
320
339
1 NaN Mark Reg 130 60
321
340
2 2.0 Faye Raker 130 60
322
341
323
- >>> data = [{'state': 'Florida',
324
- ... 'shortname': 'FL',
325
- ... 'info': {'governor': 'Rick Scott'},
326
- ... 'counties': [{'name': 'Dade', 'population': 12345},
327
- ... {'name': 'Broward', 'population': 40000},
328
- ... {'name': 'Palm Beach', 'population': 60000}]},
329
- ... {'state': 'Ohio',
330
- ... 'shortname': 'OH',
331
- ... 'info': {'governor': 'John Kasich'},
332
- ... 'counties': [{'name': 'Summit', 'population': 1234},
333
- ... {'name': 'Cuyahoga', 'population': 1337}]}]
334
- >>> result = pd.json_normalize(data, 'counties', ['state', 'shortname',
335
- ... ['info', 'governor']])
342
+ >>> data = [
343
+ ... {
344
+ ... "state": "Florida",
345
+ ... "shortname": "FL",
346
+ ... "info": {"governor": "Rick Scott"},
347
+ ... "counties": [
348
+ ... {"name": "Dade", "population": 12345},
349
+ ... {"name": "Broward", "population": 40000},
350
+ ... {"name": "Palm Beach", "population": 60000},
351
+ ... ],
352
+ ... },
353
+ ... {
354
+ ... "state": "Ohio",
355
+ ... "shortname": "OH",
356
+ ... "info": {"governor": "John Kasich"},
357
+ ... "counties": [
358
+ ... {"name": "Summit", "population": 1234},
359
+ ... {"name": "Cuyahoga", "population": 1337},
360
+ ... ],
361
+ ... },
362
+ ... ]
363
+ >>> result = pd.json_normalize(
364
+ ... data, "counties", ["state", "shortname", ["info", "governor"]]
365
+ ... )
336
366
>>> result
337
367
name population state shortname info.governor
338
368
0 Dade 12345 Florida FL Rick Scott
@@ -341,8 +371,8 @@ def _json_normalize(
341
371
3 Summit 1234 Ohio OH John Kasich
342
372
4 Cuyahoga 1337 Ohio OH John Kasich
343
373
344
- >>> data = {'A' : [1, 2]}
345
- >>> pd.json_normalize(data, 'A' , record_prefix=' Prefix.' )
374
+ >>> data = {"A" : [1, 2]}
375
+ >>> pd.json_normalize(data, "A" , record_prefix=" Prefix." )
346
376
Prefix.0
347
377
0 1
348
378
1 2
0 commit comments