title: Passing generic pandas dataframe to django REST API
body: <p>I am currently building a REST API with the django and djangorestframework libraries. I want to make a POST that accepts a JSON-ified version of a pandas dataframe, so that I can then call some other library function on the dataframe and return some output. </p>
  
  <p>I know that if I know the table structure of the dataframe, there are nice libraries like <a href="https://github.com/wq/django-rest-pandas" rel="nofollow noreferrer">django-rest-pandas</a> that can serialize and return pandas dataframes as JSON, but I am not sure about how to accept them as parameters to a function.</p>
  
  <p>My current thought was to pass the JSON as a string to a function-based view, and from there, extract and parse the JSON as a pandas dataframe. However, I don't know if this is the best way to do this. My biggest complication is that I don't know the structure of the dataframe beforehand, so I am not sure how to incorporate that into a model that perhaps some library is able to interpret (although I am still interested in a way to do this if I did know the structure).</p>
  
  <p>Here is my code so far.</p>
  
  <p>Sample POST request:</p>
  
  <pre><code>{
      "table": "{\"AAPL\":{\"1420156800000\":103.8639572404,\"1420416000000\":100.9379443592,\"1420502400000\":100.947444401,\"1420588800000\":102.3629506325,\"1420675200000\":106.2959679468},\"AMZN\":{\"1420156800000\":308.52,\"1420416000000\":302.19,\"1420502400000\":295.29,\"1420588800000\":298.42,\"1420675200000\":300.46},\"FB\":{\"1420156800000\":78.45,\"1420416000000\":77.19,\"1420502400000\":76.15,\"1420588800000\":76.15,\"1420675200000\":78.175},\"GOOG\":{\"1420156800000\":524.81,\"1420416000000\":513.87,\"1420502400000\":501.96,\"1420588800000\":501.1,\"1420675200000\":502.68},\"MSFT\":{\"1420156800000\":43.3443122802,\"1420416000000\":42.9410878182,\"1420502400000\":42.3153946875,\"1420588800000\":42.8530273035,\"1420675200000\":44.1136830927}}",
      "num_samples": 10
  }
  </code></pre>
  
  <p>models.py:</p>
  
  <pre><code>class EFObject(models.Model):
      table = models.TextField()
      num_samples = models.IntegerField()
  </code></pre>
  
  <p>serializers.py:</p>
  
  <pre><code>class EFObjectSerializer(serializers.ModelSerializer):
      class Meta:
          model = EFObject
          fields = ('table', 'num_samples')
  </code></pre>
  
  <p>views.py:</p>
  
  <pre><code>from rest_framework.response import Response
  from rest_framework.decorators import api_view
  from lib.api.serializers import *
  import lib.analytics.analytics as analytics
  import json
  import pandas as pd
  
  @api_view(http_method_names=['POST'])
  def find_ef(request):
      serializer = EFObjectSerializer(data=request.data)
      serializer.is_valid()
      output = serializer.validated_data['table']
      num_samples = serializer.validated_data['num_samples']
      table = pd.read_json(output) 
      result = analytics.find_ef(table, num_samples) # returns a dictionary
      return Response(result)
  </code></pre>
  
  <p>urls.py:</p>
  
  <pre><code>from django.conf.urls import include, url
  from rest_framework import routers
  from lib.api import views
  
  router = routers.DefaultRouter()
  router.register(r'users', views.UserViewSet)
  
  # Wire up our API using automatic URL routing.
  # Additionally, we include login URLs for the browsable API.
  urlpatterns = [
      url(r'', include(router.urls)),
      url(r'api-auth/', include('rest_framework.urls', namespace='rest_framework')),
      url(r'ef', views.find_ef)
  ]
  </code></pre>
  
  <p>This works, but my question is what I should really be doing in the view.py file, as this doesn't seem like the best way to get a dataframe (and it's also a hassle to correctly escape the JSON string of the dataframe after calling <code>table.to_json()</code>). Any help would be appreciated.</p>