Coder
Coder

Reputation: 1121

list of Json to dataframe in python

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.

Upvotes: 1

Views: 142

Answers (1)

mozway
mozway

Reputation: 260360

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

Upvotes: 2

Related Questions