Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I put separate row of dictionary into Dataframe and export to csv file?

Data is Dictionary in a separate line.
How can I put it together into Dataframe and export to civ file?

json_responses = response.text.split('\n')
for item in json_responses:
    if item != '':
        dic = json.loads(item)
        print(dic)
{'sku_id': 'A', 'primary_category_code': 'AA52103320001', 'primary_store': 'F0001001'}
{'sku_id': 'B', 'primary_category_code': 'AA52103320001', 'primary_store': 'F0001001'}
{'sku_id': 'C', 'primary_category_code': 'AA52103320002', 'primary_store': 'F0001002'}
{'sku_id': 'D', 'primary_category_code': 'AA52103320002', 'primary_store': 'F0001002'}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can collect the dicts in a list and pass it to DataFrame constructor.

import json
lst_of_dic = [json.loads(item) for item in response.text.split('\n') if item != '']
df = pd.DataFrame(lst_of_dic)

Output:

  sku_id primary_category_code primary_store
0      A         AA52103320001      F0001001
1      B         AA52103320001      F0001001
2      C         AA52103320002      F0001002
3      D         AA52103320002      F0001002
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading