How do I convert a list to dictionary with specific format in python?

GeekyDad

I'm developing a Web API, I have a list from a query results like this:

[{'ORG': 'Asset Management',
  'SURVEY_DATE': datetime.date(2018, 4, 23),
  'NOS': '1'},
 {'ORG': 'Asset Management',
  'SURVEY_DATE': datetime.date(2018, 5, 8),
  'NOS': '1'},
 {'ORG': 'Chief Advocacy Office',
  'SURVEY_DATE': datetime.date(2018, 10, 31),
  'NOS': '50'},
 {'ORG': 'Chief Advocacy Office',
  'SURVEY_DATE': datetime.date(2019, 2, 13),
  'NOS': '1'},
 {'ORG': 'Chief Information Office',
  'SURVEY_DATE': datetime.date(2018, 1, 22),
  'NOS': '1'},
 {'ORG': 'Chief Information Office',
  'SURVEY_DATE': datetime.date(2018, 2, 2),
  'NOS': '1'}]

I tried to convert it first into a dataframe and code it like this:

 df1 = df1.groupby('ORG').apply(lambda x: dict(zip(x['SURVEY_DATE'],x['NOS']))).to_dict()

but is there a way that I dont need to convert it in dataframe?

And I want to format it into a dictionary like the one below for my response data:

{
  "Asset Management": [
    {
      "date": "2019-03-30",
      "numberOfSurveys": 76
    },
    {
      "date": "2019-03-31",
      "numberOfSurveys": 83
    }
  ],
  "Chief Advocacy Office": [
   {
      "date": "2019-03-30",
      "numberOfSurveys": 50
   },
   {
     "date": "2019-03-31",
     "numberOfSurveys": 40
   }
  ],
  "Chief Information Office": [
   {
       "date": "2019-03-30",
      "numberOfSurveys": 50
   },
   {
      "date": "2019-03-31",
      "numberOfSurveys": 40
   }
  ]
}
Markus Meskanen

Use collections.defaultdict():

import collections

result = collections.defaultdict(list)
for row in original_data:
    result[row['ORG']].append({
        'date': row['SURVEY_DATE'],
        'numberOfSurveys': row['NOS'],
    })

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I convert a list of lists to a python dictionary of the following format?

How do I convert list into dictionary in Python?

Python: How do I format a loop to pull specific values in a dictionary list?

how do i convert a string to specific format

How do I convert a list/dictionary into a Dataframe?

How do I convert a list of tuples to a dictionary

How can I convert a column of Excel into a specific dictionary format?

How do I format this dictionary in python?

How do I concisely convert a python dictionary to a list of tuples, given a dictionary in which the values may be different types?

Convert a dictionary of list to a dataframe in a specific format

How do I convert this dictionary to a dataframe in python?

How can i convert my python dictionary to below format

How do i format a value as a list from a dictionary

Python output list of dictionary in specific format

How do I convert a list with values of different lengths into a dictionary?

How do i convert a dictionary with unequal list as value to csv

How do i convert list with word count into a dictionary

How do I convert a list into a dictionary grouped by object name?

Convert python list of lists into dictionary format

Python-Convert a list to dictionary with specific rule

Inability to convert a specific part of the output of the Java script code to Python dictionary or list format

python dictionary and list: how to convert it?

how to convert a list to dictionary in python?

How to convert list to dictionary in Python?

How do I write a dictionary info a text file in this format? (Python)

How can I convert a list to JSON/dictionary - Python

How do I convert my output to list format?

How do I access a specific value in a nested Python dictionary?

Reupload, How do i convert a php file to python dictionary?