- a virtual environment
- a working ClickHouse Cloud service and your connection details
- Connect to ClickHouse Cloud from Jupyter notebooks using chDB
- Query remote datasets and convert results to Pandas DataFrames
- Combine cloud data with local CSV files for analysis
- Visualize data using matplotlib
Setup
To add this dataset to an existing ClickHouse Cloud service, login to console.clickhouse.cloud with your account details. In the left hand menu, click onData sources. Then click Predefined sample data:
Select Get started in the UK property price paid data (4GB) card:
Then click Import dataset:
ClickHouse will automatically create the pp_complete table in the default database and fill the table with 28.92 million rows of price point data.
In order to reduce the likelihood of exposing your credentials, we recommend to add your Cloud username and password as environment variables on your local machine.
From a terminal run the following command to add your username and password as environment variables:
The environment variables above persist only as long as your terminal session.
To set them permanently, add them to your shell configuration file.
localhost:8888.
Click File > New > Notebook to create a new Notebook.
You will be prompted to select a kernel.
Select any Python kernel available to you, in this example we will select the ipykernel:
In a blank cell, you can type the following command to install chDB which we will be using connect to our remote ClickHouse Cloud instance:
Exploring the data
With the UK price paid data set up and chDB up and running in a Jupyter notebook, we can now get started exploring our data. Let’s imagine we’re interested in checking how price has changed with time for a specific area in the UK such as the capital city, London. ClickHouse’sremoteSecure function allows you to easily retrieve the data from ClickHouse Cloud.
You can instruct chDB to return this data in process as a Pandas data frame - which is a convenient and familiar way of working with data.
Write the following query to fetch the UK price paid data from your ClickHouse Cloud service and turn it into a pandas.DataFrame:
chdb.query(query, "DataFrame") runs the specified query and outputs the result to the terminal as a Pandas DataFrame.
In the query we’re using the remoteSecure function to connect to ClickHouse Cloud.
The remoteSecure functions takes as parameters:
- a connection string
- the name of the database and table to use
- your username
- your password
remoteSecure function connects to the remote ClickHouse Cloud service, runs the query and returns the result.
Depending on the size of your data, this could take a few seconds.
In this case we return an average price point per year, and filter by town='LONDON'.
The result is then stored as a DataFrame in a variable called df.
df.head displays only the first few rows of the returned data:
Run the following command in a new cell to check the types of the columns:
date is of type Date in ClickHouse, in the resulting data frame it is of type uint16.
chDB automatically infers the most appropriate type when returning the DataFrame.
With the data now available to us in a familiar form, let’s explore how prices of property in London have changed with time.
In a new cell, run the following command to build a simple chart of time vs price for London using matplotlib:
file table engine to read files directly on your local machine.
In a new cell, run the following command to make a new DataFrame from the local .csv file.
Read from multiple sources in a single step
Read from multiple sources in a single step
It’s also possible to read from multiple sources in a single step. You could use the query below using a
JOIN to do so:Summary
This guide demonstrated how chDB enables seamless data exploration in Jupyter notebooks by connecting ClickHouse Cloud with local data sources. Using the UK Property Price dataset, we showed how to query remote ClickHouse Cloud data with theremoteSecure() function, read local CSV files with the file() table engine, and convert results directly to Pandas DataFrames for analysis and visualization.
Through chDB, data scientists can leverage ClickHouse’s powerful SQL capabilities alongside familiar Python tools like Pandas and matplotlib, making it easy to combine multiple data sources for comprehensive analysis.
While many a London-based data scientist may not be able to afford their own home or apartment any time soon, at least they can analyze the market that priced them out!