Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert List to DataFrame and Split nested dictionary inside DataFrame column - Python 3.6

I want to convert List to DataFrame and Split nested dictionary inside DataFrame column, below is my code.

import pandas

d = [{"key":1000000, "name":"Aelf", "level":0, "hasData":"true", "fields":{"id":"elf"}}]
pd.DataFrame(d)

I want to split "fields" into new column "id"

key name    level   hasData fields  id
1000000 Aelf    0   true    {'id': 'elf'}   elf
like image 442
Learnings Avatar asked Apr 24 '26 00:04

Learnings


2 Answers

You can use join with pop + tolist:

d = [{"key":1000000, "name":"Aelf", "level":0, "hasData":"true", "fields":{"id":"elf"}},
     {"key":2000000, "name":"Cdaf", "level":1, "hasData":"false", "fields":{"id":"deer"}}]
df = pd.DataFrame(d)

res = df.join(pd.DataFrame(df.pop('fields').tolist()))

Result:

  hasData      key  level  name    id
0    true  1000000      0  Aelf   elf
1   false  2000000      1  Cdaf  deer
like image 93
jpp Avatar answered Apr 25 '26 13:04

jpp


Using concat

pd.concat([df,df.fields.apply(pd.Series)],axis=1)
Out[308]: 
          fields hasData      key  level  name   id
0  {'id': 'elf'}    true  1000000      0  Aelf  elf
like image 27
BENY Avatar answered Apr 25 '26 12:04

BENY



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!