0

I have created a Pivot table and now need to convert it do a Django table.

I get this data with this:

df = pd.pivot_table ( df, index = [  "category" ], columns=['date'], values = ['comm'],  aggfunc = np.sum, fill_value=0 )

which gives me this:

                      comm                                            
date            2013-12-28 2014-12-27 2015-12-25 2016-12-31 2017-05-20
category                                                              
CONT ASSET FEES    3868.32    4450.94    6063.94    5285.85   17479.07
FIXED ANN TRAIL    1299.94    1299.94    1277.24    1223.70    1848.56
....
INSURANCE          5132.08    6017.77    1672.13          0    5059.51
INSURANCE TRAIL     935.05     701.68     623.86     458.45    1357.83

send to the template with this:

context = {
    'annual'     : df.to_records (),
}

Now need to convert it to a Django table. I am guessing ".comm" will be part of the code.

      {% for c in annual %}

        <tr>
          <td>{{ c.category }}</td>
          <td>{{ c.comm|floatformat:"0"|intcomma }}</td>
          <td>{{ c.comm|floatformat:"0"|intcomma }}</td>
          <td>{{ c.comm|floatformat:"0"|intcomma }}</td>
          <td>{{ c.comm|floatformat:"0"|intcomma }}</td>
          <td>{{ c.comm|floatformat:"0"|intcomma }}</td>
        </tr>

      {% endfor %}

obviously I cant you the data string.

what can I do to get this to print?

Thanks!

diogenes
  • 1,865
  • 3
  • 24
  • 51

1 Answers1

0

In my opinion you can prevent MultiIndex in columns by remove []:

df = df.pivot_table(index = "category" , 
                    columns='date', 
                    values = 'comm', 
                    aggfunc = np.sum, 
                    fill_value=0)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • I got this to work when printing out in the template, <"td">{{ c.1 }}. So I did c.1, c,2, c3, etc... works but I am sure there is a better way, – diogenes Apr 05 '19 at 08:41
  • @diogenes - django side is hard for me, never do it. I try find some solutions [here](https://stackoverflow.com/questions/39003732/display-django-pandas-dataframe-in-a-django-template) and [this](https://stackoverflow.com/questions/34425607/how-to-write-a-pandas-dataframe-to-django-model)... – jezrael Apr 05 '19 at 08:51