Dataframe 如何将presto查询输出转换为python数据帧
我想将我的查询输出转换为python数据帧以绘制线图Dataframe 如何将presto查询输出转换为python数据帧,dataframe,presto,Dataframe,Presto,我想将我的查询输出转换为python数据帧以绘制线图 import prestodb import pandas as pd conn=prestodb.dbapi.connect( host='10.0.0.101', port=8081, user='hive', catalog='hive', schema='ong', ) cur = conn.cursor() query="SELECT dtime,tagName FROM machine where tagname is no
import prestodb
import pandas as pd
conn=prestodb.dbapi.connect(
host='10.0.0.101',
port=8081,
user='hive',
catalog='hive',
schema='ong',
)
cur = conn.cursor()
query="SELECT dtime,tagName FROM machine where tagname is not null
limit 1000"
cur.execute(query)
rows = cur.fetchall()
print(rows)
df = pd.DataFrame(query, columns=['x_axis','tagName'])
这是我的查询输出示例
[['2018-09-08 00:00:00.000', 26], ['2018-09-08 01:00:00.000', 26],
['2018-09-08 02:00:00.000', 26], ['2018-09-08 03:00:00.000', 27],
['2018-09-08 04:00:00.000', 27], ['2018-09-08 05:00:00.000', 27]]
如何使用python将此查询输出转换为数据帧非常简单,我建议您使用pyhive.presto连接器请参见:,以连接到presto,但您使用的连接器也应以相同的方式工作 然后你有两个选择: 1-使用presto连接和读取sql查询 2-使用presto游标并使用fetchall的输出作为数据帧的输入数据 选择1 作为pd进口熊猫 从pyhive导入presto connection=presto.connectuser='my-user',host='presto.my.host.com',port=8889 df=pd.read\u sql\u queryselect 100,连接 打印 测向头 或 选择2 作为pd进口熊猫 从pyhive导入presto connection=presto.connectuser='my-user',host='presto.my.host.com',port=8889 cur=connection.cursor 当前执行选择100 df=pd.DataFramecur.fetchall 打印 测向头
df=pd.DataFramecur.fetchall printdf