Forums

Python + Bottle + Pandas Dataframe

Hi! i would like try to print a pandas dataframe after execute Mysql query

 def checkstatus():
     conn = MySQLdb.connect(host=dbHost, user=dbUser, passwd=dbPasswd, db=dbDb)
     cursor = conn.cursor()
     return cursor

 def checkstatusquerytest(cursor):
     cursor.execute(querycheckstatus)
     rows = cursor.fetchall()
     df = pd.DataFrame( [[ij for ij in i] for i in rows] )
     df.rename(columns={0: 'id', 1: 'status'}, inplace=True)
     #df = df.sort(['id'], ascending=[1])    --> deprecated
     df = df.sort_values(by="id")
     return df

But i have the next message in error.log

 2018-04-10 19:41:32,115: <h1>Critical error while processing request: /check</h1>Error running WSGI application
 2018-04-10 19:41:32,124: ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
 2018-04-10 19:41:32,124:   File "/usr/local/lib/python2.7/dist-packages/bottle.py", line 954, in wsgi
 2018-04-10 19:41:32,124:     out = self._cast(self._handle(environ))
 2018-04-10 19:41:32,125: 
 2018-04-10 19:41:32,125:   File "/usr/local/lib/python2.7/dist-packages/bottle.py", line 887, in _cast
 2018-04-10 19:41:32,125:     if not out:
 2018-04-10 19:41:32,125: 
 2018-04-10 19:41:32,125:   File "/home/adriezemartinez/.local/lib/python2.7/site-packages/pandas/core/generic.py", line 1121, in __nonzero__
 2018-04-10 19:41:32,125:     .format(self.__class__.__name__))

Bottle has issues with Pandas Dataframe?

Thanks in advance! :D

I might be misunderstanding, but if you want to return a DataFrame from a website's code, you'll probably need to format it somehow -- that is, generate some HTML to represent it.

i've tried to code a rest api (using bottle and mysql) for data analysis porpouses.

The select row results are added to Panda's Dataframe, so i can use (and later) print them.

When i run this script in console, the dataframe is printed in output without issues, but when i display it in page, i got the error :(

Maybe bottle, is not an option?

solved :D thanks Giles :D:D:D:D:D:D

Excellent, glad you worked it out!