Python 熊猫-解析MySql结果

Python 熊猫-解析MySql结果,python,mysql,pandas,dataframe,Python,Mysql,Pandas,Dataframe,我正在使用python在MySql上执行查询。在UI端使用FLOAPI表示MySql数据。以下是目前的实施情况 query2 = f"""select time, value from NWSTAT where time > \'{from_date}\' and time < \'{to_date}\'""" result2 = pd.read_sql(query2, engine) return result2.to_jso

我正在使用python在MySql上执行查询。在UI端使用FLOAPI表示MySql数据。以下是目前的实施情况

query2 = f"""select time, value from NWSTAT 
             where time > \'{from_date}\' 
             and time < \'{to_date}\'"""
result2 = pd.read_sql(query2, engine)
return result2.to_json(orient='records')
根据这个响应,我在UI Javascript端为Flot API创建了belwo结构

[[1581931200000,0],[1581931200000,0],[1581931200000,0],[1581931200000,0]]
有没有办法在python端本身不进行任何迭代就能做到这一点?直接从查询结果

使用Flask服务器。 UI端:JQuery、Handlebar JS

编辑:在接受答案中,第二种方法花费的时间较少。。以下是两种方法处理240k记录所用的时间

 First one: --- 1.6689300537109375e-06 seconds ---
 Second one: --- 0.5330650806427002 seconds ---

问题是,如果将两列转换为整数的numpy数组格式更改为浮点值

print (json.dumps(result2.to_numpy().tolist()))
第一个想法是从字典的
.values()
创建列表,并转换为
json

import json

query2 = f"""select time, value from NWSTAT 
             where time > \'{from_date}\' 
             and time < \'{to_date}\'"""
result2 = pd.read_sql(query2, engine)
return json.dumps([list(x.values()) for x in result2.to_dict(orient='records')])
import json

query2 = f"""select time, value from NWSTAT 
             where time > \'{from_date}\' 
             and time < \'{to_date}\'"""
result2 = pd.read_sql(query2, engine)
return json.dumps(list(map(list, zip(*result2.to_dict(orient='l').values()))))

感谢更新的解决方案,第二个解决方案在更短的时间内给出相同的结果。有没有处理NaN值的选项?@saravanakumar-如果NaN值是多少?删除行?我在mysql端处理过!!!!从NWSTAT中选择时间、合并(值,0)。。。。这解决了我的问题。。。非常感谢您及时的帮助!!!检查240K记录的响应时间==第一种方法--1.6689300537109375e-06秒---==第二种方法--0.5330650806427002秒---
import json

query2 = f"""select time, value from NWSTAT 
             where time > \'{from_date}\' 
             and time < \'{to_date}\'"""
result2 = pd.read_sql(query2, engine)
return json.dumps(list(map(list, zip(*result2.to_dict(orient='l').values()))))