title: Load Json Script with Flask Request, convert to pandas Dataframe and back to Json as Flask Response
body: <p>I am using Python/pickle for my scoring model. The data to be scored will come from a Flask Request as a Json Script. Need to read the data from a Flask Request, convert data to a pandas dataframe, transform the data via pandasql, call on the scoring model and output the scoring results dataframe as a Flask Response in Json.</p>
  
  <ol>
  <li>I can get everything above to work when the incoming data is a Json file on my hard drive (using json.load()). </li>
  <li>For debugging, i can use a Flask request to load the Json script as well as convert it to a dataframe and return the dataframe as a Flask Response in Json.</li>
  <li><strong>HOWEVER,</strong> after loading the Json script via Flask Request, i attempt to use <strong>pandasql</strong> to transform the data but get the Error the dataframe is not found "Exception: df not found". 
  
  <ul>
  <li>How can i solve this issue? Thank you in advance (code and Json script below)</li>
  </ul></li>
  </ol>
  
  <p>Per #2 above: this code works.</p>
  
  <pre><code>app = Flask(__name__)
  
  @app.route('/results', methods=['POST'])
  def load():
      data = request.get_json(force=True)
      df = pd.io.json.json_normalize(data)
      df.columns = df.columns.map(lambda x: x.split(".")[-1])
  
      resp = make_response(df.to_json())
      return resp
  
  if __name__ == '__main__':
      app.run(debug=True)
  </code></pre>
  
  <p><strong>Per Issue #3: This code does not work! Using pandasql to transform dataframe gets the Error: 'df is not found' (full traceback at bottom)</strong></p>
  
  <pre><code>app = Flask(__name__)
  
  @app.route('/results', methods=['POST'])
  def load():
      data = request.get_json(force=True)
      df = pd.io.json.json_normalize(data)
      df.columns = df.columns.map(lambda x: x.split(".")[-1])
  
  # set up query to transform data (simple example)
      q = """ 
          Select *
          from df 
          """
      query = pandasql.sqldf(q, globals())
  
      resp = make_response(query.to_json())
      resp.mimetype = 'application/json'
      return resp
  
  if __name__ == '__main__':
      app.run(debug=True)
  </code></pre>
  
  <p>Full Error traceback:</p>
  
  <pre><code>File "C:\ app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  line 1817, in wsgi_app
    response = self.full_dispatch_request()
  line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  line 52, in load
    query = pandasql.sqldf(q, globals())
  line 108, in sqldf
    raise Exception("%s not found" % table)
  Exception: df not found
  </code></pre>
  
  <p>Json Script:</p>
  
  <pre><code>{
      "response":{
        "version":"1.1",
        "token":"dsfgf",
         "body":{
           "customer":{
               "customer_id":"1234567",
               "verified":"true"
          },
           "contact":{
               "email":"mr@abc.com",
               "mobile_number":"0123456789"
          },
           "personal":{
               "gender": "m",
               "title":"Dr.",
               "last_name":"Muster",
               "first_name":"Max",
               "family_status":"single",
               "dob":"1985-12-23"
              }
          }
      }
  }
  </code></pre>