Home > Enterprise >  list of Json to dataframe in python
list of Json to dataframe in python

Time:02-02

I want to convert a list of JSON strings into a proper DataFrame.

input:

x=['{"a":{"df":"9.0"},"b":{"df":"1234"}}',
   '{"a":{"fg":"3.0"},"b":{"fg":"1234"}}']

expected output:

a       t        b
9.0     df      1234 
3.0     fg      1234 

I tried the following codes but it did not work.

pd.DataFrame.from_records(x)
pd.DataFrame.from_dict(x)

Maybe its requires iteration, I am not sure about that.

CodePudding user response:

One option is to use pandas.read_json and pandas.concat:

pd.concat(map(pd.read_json, x)).rename_axis('t').reset_index()

output:

    t  a     b
0  df  9  1234
1  fg  3  1234
  •  Tags:  
  • Related