python-dictionary-to-pandas-data-frame

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

``` {.sourceCode .python} 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)

<div style="max-height:1000px;max-width:1500px;overflow:auto;">
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>123</th>
      <th>345</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td> the</td>
      <td> little</td>
    </tr>
    <tr>
      <th>1</th>
      <td> cat</td>
      <td>    boy</td>
    </tr>
    <tr>
      <th>2</th>
      <td> sat</td>
      <td>   blue</td>
    </tr>
    <tr>
      <th>3</th>
      <td>  on</td>
      <td>   blew</td>
    </tr>
    <tr>
      <th>4</th>
      <td> the</td>
      <td>    his</td>
    </tr>
    <tr>
      <th>5</th>
      <td> mat</td>
      <td>   horn</td>
    </tr>
  </tbody>
</table>
</div>
Well this is a start\.... but not what I am really looking for.
---------------------------------------------------------------

``` {.sourceCode .python}
#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))

``` {.sourceCode .python} word_freq_list

::: {.parsed-literal}

\[(345, \'little\'),

:   (345, \'boy\'), (345, \'blue\'), (345, \'blew\'), (345, \'his\'),
    (345, \'horn\'), (123, \'the\'), (123, \'cat\'), (123, \'sat\'),
    (123, \'on\'), (123, \'the\'), (123, \'mat\')\]
:::

``` {.sourceCode .python}
df2=pd.DataFrame(word_freq_list)
df2.columns=(['period','word'])
df2['freq']=1

``` {.sourceCode .python} df2.head()

<div style="max-height:1000px;max-width:1500px;overflow:auto;">
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>period</th>
      <th>word</th>
      <th>freq</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td> 345</td>
      <td> little</td>
      <td> 1</td>
    </tr>
    <tr>
      <th>1</th>
      <td> 345</td>
      <td>    boy</td>
      <td> 1</td>
    </tr>
    <tr>
      <th>2</th>
      <td> 345</td>
      <td>   blue</td>
      <td> 1</td>
    </tr>
    <tr>
      <th>3</th>
      <td> 345</td>
      <td>   blew</td>
      <td> 1</td>
    </tr>
    <tr>
      <th>4</th>
      <td> 345</td>
      <td>    his</td>
      <td> 1</td>
    </tr>
  </tbody>
</table>
</div>
It\'s getting there\...\...
===========================

``` {.sourceCode .python}
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

``` {.sourceCode .python} df3=df2.drop_duplicates().pivot('period','word','freq')

``` {.sourceCode .python}
df3=df3.fillna(0)

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

``` {.sourceCode .python} df3

<div style="max-height:1000px;max-width:1500px;overflow:auto;">
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th>word</th>
      <th>blew</th>
      <th>blue</th>
      <th>boy</th>
      <th>cat</th>
      <th>his</th>
      <th>horn</th>
      <th>little</th>
      <th>mat</th>
      <th>on</th>
      <th>sat</th>
      <th>the</th>
    </tr>
    <tr>
      <th>period</th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>123</th>
      <td> 0</td>
      <td> 0</td>
      <td> 0</td>
      <td> 1</td>
      <td> 0</td>
      <td> 0</td>
      <td> 0</td>
      <td> 1</td>
      <td> 1</td>
      <td> 1</td>
      <td> 1</td>
    </tr>
    <tr>
      <th>345</th>
      <td> 1</td>
      <td> 1</td>
      <td> 1</td>
      <td> 0</td>
      <td> 1</td>
      <td> 1</td>
      <td> 1</td>
      <td> 0</td>
      <td> 0</td>
      <td> 0</td>
      <td> 0</td>
    </tr>
  </tbody>
</table>
</div>
It would be nicer - if the columns were sorted \.... so
=======================================================

``` {.sourceCode .python}
df3=df3[sorted(df3.columns.tolist())]

{.sourceCode .python} 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