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

SQL output to a dictionary of list

I am trying to convert the following o/p of my SQL query (as a dataframe) to a dictionary of list:

Column A   Column B
M             N
M             O

I want the result in following format:

{M:[N,O]}

Can someone help me in this?

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

>Solution :

You could try like this:

a_dict = df.groupby('Column A', as_index=True)['Column B'].apply(list).to_dict()

print(a_dict)
{'M': ['N', 'O']}
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