p.magalhaes
p.magalhaes

Reputation: 8364

Using pandas with json normalize

I am trying to normalize a very simple JSON structure.

data = [{"pedido": {"situacao": "OK", ....}}, {"pedido": {"situacao": "NOK", ...}}]
rs = json_normalize(data, 'pedido', [['pedido', 'situacao']])

I would like to get just the index and another column called pedido.situacao. There is a bunch of other field on json but I want to get only situacao:

0 pedido.situacao
0  situacao              OK
1  situacao             NOK

It seens there is a extra column with "0"as label.

Upvotes: 1

Views: 70

Answers (2)

NYC Coder
NYC Coder

Reputation: 7594

You can just do this:

rs = pd.json_normalize(data)
print(rs[['pedido.situacao']])

  pedido.situacao
0              OK
1             NOK

Upvotes: 0

BENY
BENY

Reputation: 323226

We can do

df=pd.concat(pd.DataFrame(x) for x in data)
         pedido
situacao     OK
situacao    NOK

Update

df=pd.concat([pd.DataFrame(x) for x in data],keys=range(len(data))).unstack(level=1)
df.columns=df.columns.map('.'.join)
df
  pedido.situacao
0              OK
1             NOK

Upvotes: 1

Related Questions