用python中的字典制作数据框

时间:2018-10-03 19:39:06

标签: python dictionary dataframe

尝试根据以下命令创建数据库:

    sales = { 
   "clients": [
       {"ID_client": "241341", 
       "purchases": [
            "Item 101",
            "Item 202",
            "Item 324",
        ],
        "payment": [
            "visa", "master", "visa"
        ]           
       },
       {"ID_client": "24356", 
       "purchases": [
            "Item 2320",
            "Item 2342",
            "Item 5604",
        ],
        "payment": [
            "diners", "cash", "diners"
        ]           
       },    
       {"ID_client": "5534", 
       "purchases": [
            "Item 50563",
            "Item 52878",
            "Item 54233",
        ],
        "payment": [
            "diners", "master", "visa"
        ]           
       }       
   ]

}

到目前为止,我有:

    all=[]
row_purchase=[]
for p1 in sales['clients'] :
    for p2,p3 in zip(p1['purchases'],p1['payment']):
        row_p1=p1['ID_client']+","+p2+","+p3
        row_purchase.append(row_p1)
all.append(row_purchase)

df = pd.DataFrame(np.array(all), columns = ['ID_client','purchase','payment'])

我有:

[[['241341,Item 101,visa','241341,Item 202,master','241341,Item 324,visa','24356,Item 2320,diners','24356,Item 2342,cash', '24356,Item 5604,diners','5534,Item 50563,diners','5534,Item 52878,master','5534,Item 54233,visa']]

创建df时出错。

我需要创建以下df:

enter image description here

任何帮助都将非常受欢迎。 预先感谢。

1 个答案:

答案 0 :(得分:2)

假设给定客户的付款次数等于购买次数,则可以将列表理解与zip一起使用。

>>> pd.DataFrame(
        [(record['ID_client'], purchase, payment)
         for record in sales['clients']
         for purchase, payment in zip(record['purchases'], record['payment'])],
        columns=['ID_client', 'Purchase', 'Payment'])
  ID_client    Purchase Payment
0    241341    Item 101    visa
1    241341    Item 202  master
2    241341    Item 324    visa
3     24356   Item 2320  diners
4     24356   Item 2342    cash
5     24356   Item 5604  diners
6      5534  Item 50563  diners
7      5534  Item 52878  master
8      5534  Item 54233    visa

请参考以下相关问题:How to explode a list inside a Dataframe cell into separate rows