Python Dictionary to Pandas data frame

import pandas as pd

I have a dictionary with seconds - and then words I have seem

We need to move them into pandas.... but How ?

I will try directly using from_dict

word_freq={}
word_freq[123]=['the','cat','sat','on','the','mat']
word_freq[345]=['little','boy','blue','blew','his',
                'horn']

pd.DataFrame.from_dict(word_freq)
123 345
0 the little
1 cat boy
2 sat blue
3 on blew
4 the his
5 mat horn

Well this is a start.... but not what I am really looking for.

#Lets make a tuple - and store in an array
word_freq_list=[]
for k in word_freq:
    words=word_freq[k]
    for w in words:
        word_freq_list.append((k,w))
word_freq_list
[(345, 'little'),
 (345, 'boy'),
 (345, 'blue'),
 (345, 'blew'),
 (345, 'his'),
 (345, 'horn'),
 (123, 'the'),
 (123, 'cat'),
 (123, 'sat'),
 (123, 'on'),
 (123, 'the'),
 (123, 'mat')]
df2=pd.DataFrame(word_freq_list)
df2.columns=(['period','word'])
df2['freq']=1
df2.head()
period word freq
0 345 little 1
1 345 boy 1
2 345 blue 1
3 345 blew 1
4 345 his 1

It's getting there......

df2.set_index(['period'])
word freq
period
345 little 1
345 boy 1
345 blue 1
345 blew 1
345 his 1
345 horn 1
123 the 1
123 cat 1
123 sat 1
123 on 1
123 the 1
123 mat 1
df3=df2.drop_duplicates().pivot('period','word','freq')
df3=df3.fillna(0)

And now my dataframe looks how I need it....

df3
word blew blue boy cat his horn little mat on sat the
period
123 0 0 0 1 0 0 0 1 1 1 1
345 1 1 1 0 1 1 1 0 0 0 0

It would be nicer - if the columns were sorted .... so

df3=df3[sorted(df3.columns.tolist())]
df3
word blew blue boy cat his horn little mat on sat the
period
123 0 0 0 1 0 0 0 1 1 1 1
345 1 1 1 0 1 1 1 0 0 0 0

I hope that you found this useful

Pandas is a very flexible and useful module