【发布时间】:2021-06-14 14:45:14
【问题描述】:
我有一本这样的字典:
dictionary = {'user1':[{'product1':10}, {'product2':15}, {'product3': 20}],
'user2':[{'product1':13}, {'product2':8}, {'product3': 50}]}
我想构建一个数据框,在其中我可以看到 user1 和 user2 作为索引,product1、product2 和 product3 作为列,这些产品的值应该是列的值。我试着看这里,发现这个帖子Construct pandas DataFrame from items in nested dictionary,但我的数据格式不同,我不知道如何让产品成为我的专栏。到目前为止,我在第一行的第一列中得到了“'product1':10”作为我的值。使用 orient='index' 使我的主键成为索引,仅此而已。请帮帮我。
【问题讨论】:
-
你尝试过熊猫
json_normalize -
@BhavaniRavi,我认为这行不通
-
不,我没有。我对此并不熟悉。如果您可以提供代码示例,那将非常有帮助。谢谢。
标签: python pandas dictionary