1

Trying to make a database from the following dict:

    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"
        ]           
       }       
   ]

}

So far I have:

    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'])

And I have:

[['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']]

And an error creating the df.

I need to create the following df:

enter image description here

Any help will be very welcome. Thanks in advance.

2
  • Can you make use of pandas read_json() function? Commented Oct 3, 2018 at 19:40
  • I've tested, but I don't get the dataframe the way I need it. Commented Oct 3, 2018 at 19:48

1 Answer 1

2

Assuming that the number of payments for a given client equals the number of purchases, you can use a list comprehension together with 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

Please refer to this related question: How to explode a list inside a Dataframe cell into separate rows

Sign up to request clarification or add additional context in comments.

1 Comment

Worked just fine @Alexander! Thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.