Normalizar dados JSON simples:
import pandas as pd
data = [
{"id": 1, "name": {"first": "Coleen", "last": "Volk"}},
{"name": {"given": "Mark", "family": "Regner"}},
{"id": 2, "name": "Faye Raker"},
]
df = pd.json_normalize(data)
print(df)
Isso criará um DataFrame com as colunas id
, name.first
, name.last
, name.given
e name.family
.
Normalizar dados JSON com níveis máximos:
data = [
{"id": 1, "name": "Cole Volk", "fitness": {"height": 130, "weight": 60}},
{"name": "Mark Reg", "fitness": {"height": 130, "weight": 60}},
{"id": 2, "name": "Faye Raker", "fitness": {"height": 130, "weight": 60}},
]
df = pd.json_normalize(data, max_level=1)
print(df)
Isso criará um DataFrame com as colunas id
, name
, fitness.height
e fitness.weight
.
Normalizar dados JSON com metadados:
data = [
{
"state": "Florida",
"shortname": "FL",
"info": {"governor": "Rick Scott"},
"counties": [
{"name": "Dade", "population": 12345},
{"name": "Broward", "population": 40000},
{"name": "Palm Beach", "population": 60000},
],
},
{
"state": "Ohio",
"shortname": "OH",
"info": {"governor": "John Kasich"},
"counties": [
{"name": "Summit", "population": 1234},
{"name": "Cuyahoga", "population": 1337},
],
},
]
result = pd.json_normalize(data, "counties", ["state", "shortname", ["info", "governor"]])
print(result)
Isso criará um DataFrame com as colunas name
, population
, state
, shortname
e info.governor
.