8

I have a django app, which allows the user to upload a csv file, say a csv file of rankings of universities. I'd have to process the data that has been uploaded. For example, grey out any column which has string values and calculate the mean and std. deviation of all the values of a column. For this, I am using Pandas and converting the csv file to a pandas dataframe.

How do I display the dataset from the csv file using django? The column names cannot be hard coded because the user may upload any csv file. I checked django-tables2 and did the following

csvfile = request.FILES['csv_file']
data = pd.read_csv(csvfile.name)
context = {'loaded_data': data}
return render(request, "dataflow/table.html", context)

but I get the error ValueError: Expected table or queryset, not DataFrame

user2237511
  • 1,089
  • 2
  • 13
  • 20

2 Answers2

14

Pandas dataframe can be converted to html table by itself. You can try

csvfile = request.FILES['csv_file']
data = pd.read_csv(csvfile.name)
data_html = data.to_html()
context = {'loaded_data': data_html}
return render(request, "dataflow/table.html", context)

In the html page, use {{loaded_data | safe}} to render the table.

Community
  • 1
  • 1
Timo
  • 156
  • 4
2

django-tables2 can't handle dataframes. You have do convert it into something django-tables2 understands, i.e.:

from django_tables2.tables import Table

in the view:

csvfile = request.FILES['csv_file']
data = pd.read_csv(csvfile.name)
df_table = Table(data.to_dict(orient='list'))
context = {'df_table': df_table}
return render(request, "dataflow/table.html", context)

in the template:

{% load render_table from django_tables2 %}
{% render_table df_table %}

This was a simple example, you might need do more work on the dataframe or even subclassing the Table class.

alfonso.kim
  • 2,844
  • 4
  • 32
  • 37
  • I am getting `AttributeError: 'DataFrame' object has no attribute 'as_dict'` – user2237511 May 23 '17 at 18:13
  • my bad. the method is `to_dict`. Check the [documentation](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_dict.html) to see the possible return shapes. – alfonso.kim May 23 '17 at 18:19
  • Ok, that fixed it. But I still can't render the table. I have ` {% render_table table_data %} {% for item in table_data %} {% endfor %}` and I get the error `TypeError: 'Table' object is not iterable`
    {{ item }}
    – user2237511 May 23 '17 at 23:08
  • if you are using `django_tables2` render the table itself: `{% render_table df_table %}`. If you want more control over what is rendered, then you should think in a `Form` or something else. – alfonso.kim May 23 '17 at 23:22