Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clickhouse does not return column headers

I'm trying to get some relational data from clickhouse and play with in in pandas. It works, but pd.read_sql_query returns dataframe, where column names are the values of the first row. Instead I expected to see columns names as they named in relational table.

I tried the same with Postgress and it works properly.

cheng = create_engine('clickhouse://mylogin:[email protected]:PORT/schema')
qry2 = '''select * from myschema.mytable order by a_date desc limit 10'''

dt = pd.read_sql_query(qry, cheng)
dt

enter image description here

Header of returned dataframe columns consist values of the first row, returned from DB. I expected to see columns names instead.

like image 629
user1443993 Avatar asked Jul 19 '26 03:07

user1443993


2 Answers

Clickhouse has a python client that you can install with pip:

pip install clickhouse-connect

Getting the pandas dataframe is as simple as

client = clickhouse_connect.get_client(host, port)
df = client.query_df("select * from mytable")

More at https://clickhouse.com/docs/en/integrations/language-clients/python/driver-api/

like image 55
MoneyBall Avatar answered Jul 22 '26 03:07

MoneyBall


You can get column labels in pandas dataframes using clickhouse-driver. Example shown below.

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()

You'll see labels in the df.tail() output.

like image 42
R Hodges Avatar answered Jul 22 '26 03:07

R Hodges



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!