How can I simply separate a JSON column inside pandas:
pd.DataFrame({
'col1':[1,2],
'col2':["{'foo':1, 'bar':2, 'baz':{'foo':2, 'x':1}}",
"{'foo':3, 'bar':5, 'baz':{'foo':2, 'x':1}}"]})
col1 col2
0 1 {'foo':1, 'bar':2, 'baz':{'foo':2, 'x':1}}
1 2 {'foo':3, 'bar':5, 'baz':{'foo':2, 'x':1}}
into real columns in a simple and python way?
edit
Desired output:
pd.DataFrame({'col1':[1,2], 'foo':[1,3], 'bar':[2,5],
'baz_foo':[2,2], 'baz_x':[1,1]})
col1 foo bar baz_foo baz_x
0 1 1 2 2 1
1 2 3 5 2 1
col2actually what you are looking to parse? Instantiating the DataFrame you provide works, but taking the next step usingast.literal_evaldoesn't work because that's not a valid dictionary. Haven't tried thejsonlibrary actually...