0

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?

5
  • Hi, the sample is not valid Python Commented Oct 12, 2019 at 1:18
  • And where is Book1 and book2 show in your output ? Commented Oct 12, 2019 at 1:18
  • Hi @WeNYoBen, I didn't get you. Sorry. Can you please elaborate? Commented Oct 12, 2019 at 1:21
  • can u add the rest of the codes related to pd.DataFrame([words,counts]) Commented Oct 12, 2019 at 1:27
  • Hi @Eshaka, here you go, Commented Oct 12, 2019 at 2:13

4 Answers 4

1

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.