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 to make a nested dictionary from an SqlAlchemy output (Python)?

I have the following output from an SqlAlchemy select operator.

| device_id |     event_name      | event_count |
| :--------:| :-----------------: |:-----------:|
| 123456    | speed more than 100 |      3      |
| 123456    | speed less than 12  |      0      |
| 334455    | out of NYC          |      5      |
| 111111    | in UCSD campus      |      1      |

Now I want to save this result into a nested dictionary with the following format, and I don’t know how to do it efficiently.

I need to have a dictionary in which its keys are the device_ids, and the values are dictionaries in which the keys are event_names and the values are event_counts.

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

{'123456' : {'speed more than 100' : 3,
             'speed less than 12': 0},
 '334455' : {'out of NYC' : 5},
 '111111' : {'in UCSD campus' : 1}
}

Here is my code.

 def count_per_event_json(self, count_per_event_query_result):
        result = {}
        print(count_per_event_query_result)
        for item in enumerate(count_per_event_query_result):
            if item[0] not in result.keys():
                # result[item[0]] = {I don't know how to fill this inner dict'}
    
        return result

>Solution :

You could do this with pandas:

import pandas as pd

# Use pandas.read_sql to read the query results into a dataframe
df = pd.read_sql(select_query, con=your_db_connection)

# Use the pandas groupby function to group the dataframe by device_id
grouped_df = df.groupby('device_id')

# Convert the grouped dataframe into a nested dictionary using the to_dict method
result_dict = grouped_df.apply(lambda x: x.set_index('event_name')['event_count'].to_dict()).to_dict()
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