clickhouse不返回列标题

omvjsjqw  于 2021-07-15  发布在  ClickHouse
关注(0)|答案(3)|浏览(436)

我正在尝试从clickhouse获取一些关系数据,并在pandas中玩。它可以工作,但是pd.read\u sql\u query返回dataframe,其中列名是第一行的值。相反,我希望看到列名称与它们在关系表中的名称相同。
我用postgress做了同样的尝试,效果很好。

cheng = create_engine('clickhouse://mylogin:mypassG@domain.my:PORT/schema')
qry2 = '''select * from myschema.mytable order by a_date desc limit 10'''

dt = pd.read_sql_query(qry, cheng)
dt


返回的dataframe列的头包含从db返回的第一行的值。我希望看到列名称。

3xiyfsfu

3xiyfsfu1#

请查看这个python包:https://pypi.org/project/pandahouse/

connection = {'host': 'http://clickhouse-host:8123',
              'database': 'test'
affected_rows = to_clickhouse(df, table='name', connection=connection)

df = read_clickhouse('SELECT * FROM {db}.table', index_col='id',
                     connection=connection)
u59ebvdq

u59ebvdq2#

您可以使用clickhouse驱动程序在Dataframe中获取列标签。示例如下所示。

from clickhouse_driver import Client
import pandas
client = Client('localhost')
result, columns = client.execute('SELECT * FROM iris', 
                                 {'species': "Iris-setosa"},
                                 with_column_types=True)
df = pandas.DataFrame(result, columns=[tuple[0] for tuple in columns])
df.tail()

您将在df.tail()输出中看到标签。

yyyllmsg

yyyllmsg3#

看看这个问题:用clickhouse实现pandas.read\u sql的正确方法。
我无法在最新版本的模块上重现此行为:

sqlalchemy==1.3.16
sqlalchemy-clickhouse==0.1.5.post0
pandas==1.0.3

此代码:

import pandas as pd
from sqlalchemy import create_engine

engine = create_engine('clickhouse://default:@localhost/test')
query = 'select * from call_center'

dt = pd.read_sql_query(query, engine)

print(dt)

退货:

cc_call_center_sk cc_call_center_id  ... cc_gmt_offset cc_tax_percentage
0                  1  AAAAAAAABAAAAAAA  ...          -5.0              0.11
1                  2  AAAAAAAACAAAAAAA  ...          -5.0              0.12
2                  3  AAAAAAAACAAAAAAA  ...          -5.0              0.01
3                  4  AAAAAAAAEAAAAAAA  ...          -5.0              0.05
4                  5  AAAAAAAAEAAAAAAA  ...          -5.0              0.12
5                  6  AAAAAAAAEAAAAAAA  ...          -5.0              0.11

[6 rows x 31 columns]

pycharmDataframe视图看起来也不错:

相关问题