ramu suri
ramu suri

Reputation: 9

Dictionary to Pandas Dataframe Python

I have two python dictionaries.

Sample:

{
'hello' : 10
'phone' : 12
'sky' : 13
}

{
'hello' : 8
'phone' :15
'red' :4
}

This is the dictionary of counts of words in books 'book1' and 'book2' respectively.

How can I generate a pd dataframe, which looks like this:

   hello phone  sky  red
book1 10    12     13   NaN
book2 8     15     NaN   4

I have tried :

pd.DataFrame([words,counts])

It generated:

    hello phone  sky  red
0     10    12     13   NaN
1      8    15     NaN   4 

How can I genrate a required output?

Upvotes: 0

Views: 71

Answers (4)

Shishir Naresh
Shishir Naresh

Reputation: 763

try the below code,hope this helps

dict1 = {
'hello' : 10,
'phone' : 12,
'sky' : 13
}

dict2 = {
'hello' : 8,
'phone' :15,
'red' :4
}


import pandas as pd
df = pd.DataFrame([dict1,dict2], index=['book1','book2'])
print(df)

Ouput will be:

       hello  phone   sky  red
book1     10     12  13.0  NaN
book2      8     15   NaN  4.0

Upvotes: 1

Dani Mesejo
Dani Mesejo

Reputation: 61910

Assuming you have a list of dictionaries, you could do something like this:

import pandas as pd
from itertools import chain

data = [{
    'hello': 10,
    'phone': 12,
    'sky': 13,
},
    {
        'hello': 8,
        'phone': 15,
        'red': 4
    }]

df = pd.DataFrame(data=data, columns=set(chain.from_iterable(d.keys() for d in data)))
print(df)

Output

    sky  phone  hello  red
0  13.0     12     10  NaN
1   NaN     15      8  4.0

Upvotes: 0

M_S_N
M_S_N

Reputation: 2810

You need this:


pd.DataFrame([words, counts], index=['books1', 'books2'])

Output:

      hello phone  red  sky
books1  10   12    NaN  13.0
books2  8    15    4.0  NaN

Upvotes: 1

user11553043
user11553043

Reputation:

Use df.set_index([‘book1’, ‘book2’]). See the docs here: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.set_index.html

Upvotes: 0

Related Questions