0

I have a dictionary like this

{'community_0': 30,
'community_1': 29,
'community_2': 15,
'community_3': 16,
'community_4': 123,
'community_5': 9,
'community_6': 36,
'community_7': 71,
'community_8': 95,
'community_9': 21}

i want to convert it to a pandas dataframe. I tried pd.DataFrame.from_dict(dict, orient='index') but it gave me something else:

enter image description here

I referred to this post Convert Python dict into a dataframe but it didn't help much.

Any suggestions would be appreciated.

Itamar Mushkin
  • 2,803
  • 2
  • 16
  • 32
Taie
  • 1,021
  • 16
  • 29
  • So `pd.DataFrame(list(d.items()), columns=['Date', 'DateValue'])` not working? – jezrael Sep 23 '19 at 12:19
  • Possible duplicate of [Python dict to DataFrame Pandas - levels](https://stackoverflow.com/questions/38554706/python-dict-to-dataframe-pandas-levels) – M_S_N Sep 23 '19 at 12:19
  • actually no, giving me the same undesired result like the one before – Taie Sep 23 '19 at 12:20
  • 1
    I ran `pd.DataFrame(list(d.items()))` with `d` being the dictionary you gave, and it works on my side. What version of pandas are you using? – Akalyn Sep 23 '19 at 12:27
  • it didn't work for me. I am using python 3.5 – Taie Sep 23 '19 at 12:31
  • What is the expected output? – Dani Mesejo Sep 23 '19 at 12:35
  • I tried the methods suggested above. all giving me no result in column #2 – Taie Sep 23 '19 at 12:36
  • I applied this. it is raising the following error: ValueError: malformed node or string: {'community_0': 30, 'community_1': 29, 'community_2': 15, 'community_3': 16, 'community_4': 123, 'community_5': 9, 'community_6': 36, 'community_7': 71, 'community_8': 95, 'community_9': 21} – Taie Sep 23 '19 at 12:41
  • 1
    Hi, and welcome to Stack Overflow. I've suggested an edit to include the dictionary as code. In the future, please do not post images of code, as they are impossible to copy. To learn more, please visit: https://stackoverflow.com/help/how-to-ask – Itamar Mushkin Sep 23 '19 at 12:44
  • 1
    I can confirm @jezrael 's answer worked for me. You should display the output you get, and include the code you've tried to produce the output. – Anthony R Sep 23 '19 at 12:49

3 Answers3

1

It is possible to create a pd.Series directly out of your dictionary, and then use the .to_frame() method to turn a pd.Series into a single-column DataFrame:

import pandas as pd

d = {'community_0': 30,
'community_1': 29,
'community_2': 15,
'community_3': 16,
'community_4': 123,
'community_5': 9,
'community_6': 36,
'community_7': 71,
'community_8': 95,
'community_9': 21}

pd.Series(d).to_frame()

returns:

            0
community_0 30
community_1 29
community_2 15
community_3 16
community_4 123
community_5 9
community_6 36
community_7 71
community_8 95
community_9 21
Itamar Mushkin
  • 2,803
  • 2
  • 16
  • 32
1

It depends what type of structure you want in Dataframe.

If you want to show keys in dict as rows value, then use:

-> pd.DataFrame(pd.Series(d)).reset_index()

If you want to show keys in dict as columns, then follow below steps:

  1. First use your dictionary in a "List" like this:

    d = [ { 'community_0': 31, 'community_1': 29 'community_2': 15 } ]

  2. Then use -> pd.DataFrame.from_dict(d)

Abhishek Singla
  • 401
  • 4
  • 7
  • Your "keys in dict as columns" case is exactly what benjibat suggested below, and OP already said it did not work for them – Itamar Mushkin Sep 23 '19 at 14:10
  • mine code will work. I've tested it. Reason for this is I've mentioned dictionary in a list "d= [{" but in the given problem it is only "d={". That makes a difference. – Abhishek Singla Sep 23 '19 at 19:26
0

pd.DataFrame([d]) will make it .

benjibat
  • 21
  • 1
  • On my machine it creates a single-row dataframe, not a single column. What does it create on your machine? Note that OP (the Original Poster) asked for a single-column dataframe. – Itamar Mushkin Sep 23 '19 at 12:48
  • Yes I tried. You get a one raw DataFrame with all the values – benjibat Sep 23 '19 at 16:28