Creating Python nested dictionary lists into data frames Question

Asked 2 years ago, Updated 2 years ago, 80 views

For example, the following nested dictionary lists are listed:

a = [ {'info' : {'name' : 'hahaha', 'status' : 'True'} , 'price' : 500} ]

I'd like to change it to the following data frame.

      'name'     'status'      'price'
 0    hahaha       True          500

How do I make the code?

df = pd.DataFrame(data=a, columns=['?', '?', 'price'])

Is there a way to specify the key value of the nested dictionary immediately when specifying columns

python dataframe dictionary list pandas

2022-09-20 11:27

1 Answers

>>> a
[{'info': {'name': 'hahaha', 'status': 'True'}, 'price': 500}]

>>> a_ = [ { **d["info"], "price":d["price"] } for d in a ]
>>> a_
[{'name': 'hahaha', 'status': 'True', 'price': 500}]

>>> df = pd.DataFrame(a_)
>>> df
     name status  price
0  hahaha   True    500


2022-09-20 11:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.