title: Turning list of clusters into dictionaries
body: <p>I had this very large dataframe that I clustered into multiple clusters and subclusters until all the subclusters were split up. I used hierarchical clustering and ended up with these lists in lists in lists etc. (a chain of lists), the end is the part of the dataframe of the clusters. Small example: [[Dataframe(25 obs), Dataframe(25 obs)],[Dataframe(25 obs), Dataframe(25 obs)]], so in this example the data of 100 obs is clustered in 2 subclusters and then each subcluster again in 2 subclusters. The leafs of the clusters all contain a dataframe of 25 rows and some columns. In reality my dataframe is much larger and the list much longer, both in depth as in length itself.</p>
  <p>My problem is this: I want to turn every (sub)cluster into a dictionary containing 1. a list of the children (if it has any), 2. the number of observations in the cluster and 3. the cluster id. The list has to contain the dictionaries of the children and the number of observations has to match the sum of the number of observations of the children.</p>
  <p>The structure will look as follows:
  list(len = #children) -&gt; dictionary(size 2 or 3) -&gt; list -&gt; dictionary</p>
  <p>From the example:</p>
  <pre><code>import pandas as pd
  import numpy as np
  before = [[pd.DataFrame(np.random.randn(25, 4), columns=list('ABCD')), 
             pd.DataFrame(np.random.randn(25, 4), columns=list('ABCD'))
            ],[pd.DataFrame(np.random.randn(25, 4), columns=list('ABCD')), 
               pd.DataFrame(np.random.randn(25, 4), columns=list('ABCD'))] 
           ]
  after = [{'id' : &quot;unclustered&quot;, 'size' : 100, 'children' : [
               {'id' : &quot;1&quot;, 'size' : 50, 'children' : [
                   {'id' : &quot;3&quot;, 'size' : 25},
                   {'id' : &quot;4&quot;, 'size' : 25}
                 ]},
               {'id' : &quot;2&quot;, 'size' : 50, 'children' : [
                   {'id' : &quot;5&quot;, 'size' : 25},
                   {'id' : &quot;6&quot;, 'size' : 25}
                 ]}
               ]}
           ]
  </code></pre>