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

Create dataframe from dictionary with column names

I’m attempting to create a dataframe with dictionary keys and values in two different columns then name then name the columns with string names

Here is my dictionary:

my_dict = {'amd_0': 102, 'amd_3': 11}

Column names:

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

columns = ['column1', column2']

Desired output:

column1 column2
amd_0   102
amd_3   11

I tried from_dict but I’m having a hard time naming the columns. Any help is appreciated.

>Solution :

You can create a dataframe using the dictionary items and specify the columns list as the columns:

>>> df = pd.DataFrame(my_dict.items(), columns=columns)
>>> df
  column1  column2
0   amd_0      102
1   amd_3       11
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