python pandas binning incorrect column format list of lists
<p>I am working through an example to bin columns using pandas. I am trying to use the django graphos library to plot the distribution and in order to do this I need to convert the binned output into a list of lists. Below is a snippet of the data I start with. </p>

<pre><code>         A
1     8.78
2     9.46
3     8.78
4    10.27
5    10.37
6    12.36
7    14.56
</code></pre>

<p>then I create the bins</p>

<pre><code>bins = np.linspace(sample.A.min(), sample.A.max(), 100) 
</code></pre>

<p>then I group by and count</p>

<pre><code>groups = sample.groupby(pd.cut(sample.A, bins)).count()
</code></pre>

<p>Then I get this (first couple rows) </p>

<pre><code>                      A
A                      
(7.68, 7.799]         5
(7.799, 7.918]        0
(7.918, 8.0364]       2
(8.0364, 8.155]       0
</code></pre>

<p>When I try to convert this output I receive only the aggregated column rows and not the bins (using groups.values.tolist()). (First couple rows)</p>

<pre><code>[[5],
 [0],
 [2],
 [0],
 [9],
 [25],
</code></pre>

<p>My desired output would look like (list of lists)</p>

<pre><code> data = [
           ['Bins', 'Count'],
           ['7.68, 7.799', 1000],
           ['7.799, 7.918', 1170],
           ]
</code></pre>

<p>per the example (list example from <a href="https://github.com/agiliq/django-graphos" rel="nofollow">https://github.com/agiliq/django-graphos</a>)</p>

<p>For some reason I cannot get the list to format correctly I think it is due to some part in my cut example.</p>

<p>I was doing pretty much what you were in the comments:</p>

<pre><code>binList = groups.index.tolist()
countList = [count[0] for count in groups.values.tolist()] # groups.values.tolist() comes as a list of lists
binList = [[binStr.replace('(', '').replace(']', ''), count] for binStr, count in zip(binList, countList)]
binList = ['Bins', 'Count'] + binList
</code></pre>

<p>It's not the Mona Lisa of python, but...uh...great minds think alike?</p>

python pandas numpy