title_wip

[Edit this page](https://github.com/ClickHouse/clickhouse-docs/blob/main/docs/chdb/guides/querying-pandas.md)

[Pandas](https://pandas.pydata.org/) is a popular library for data manipulation and analysis in Python.
In version 2 of chDB, we've improved the performance of querying Pandas DataFrames and introduced the `Python` table function.
In this guide, we will learn how to query Pandas using the `Python` table function.

## Setup[​](#setup "Direct link to Setup")

Let's first create a virtual environment:

```
python -m venv .venv
source .venv/bin/activate
```

And now we'll install chDB.
Make sure you have version 2.0.2 or higher:

```
pip install "chdb>=2.0.2"
```

And now we're going to install Pandas and a couple of other libraries:

```
pip install pandas requests ipython
```

We're going to use `ipython` to run the commands in the rest of the guide, which you can launch by running:

```
ipython
```

You can also use the code in a Python script or in your favorite notebook.

## Creating a Pandas DataFrame from a URL[​](#creating-a-pandas-dataframe-from-a-url "Direct link to Creating a Pandas DataFrame from a URL")

We're going to query some data from the [StatsBomb GitHub repository](https://github.com/statsbomb/open-data/tree/master?tab=readme-ov-file).
Let's first import requests and pandas:

```
import requests
import pandas as pd
```

Then, we'll load one of the matches JSON files into a DataFrame:

```
response = requests.get(
  "https://raw.githubusercontent.com/statsbomb/open-data/master/data/matches/223/282.json"
)
matches_df = pd.json_normalize(response.json(), sep='_')
```

Let's have a look what data we'll be working with:

```
matches_df.iloc[0]
```

```
match_id                                                                  3943077
match_date                                                             2024-07-15
kick_off                                                             04:15:00.000
home_score                                                                      1
away_score                                                                      0
match_status                                                            available
match_status_360                                                      unscheduled
last_updated                                           2024-07-15T15:50:08.671355
last_updated_360                                                             None
match_week                                                                      6
competition_competition_id                                                    223
competition_country_name                                            South America
competition_competition_name                                         Copa America
season_season_id                                                              282
season_season_name                                                           2024
home_team_home_team_id                                                        779
home_team_home_team_name                                                Argentina
home_team_home_team_gender                                                   male
home_team_home_team_group                                                    None
home_team_country_id                                                           11
home_team_country_name                                                  Argentina
home_team_managers              [{'id': 5677, 'name': 'Lionel Sebastián Scalon...
away_team_away_team_id                                                        769
away_team_away_team_name                                                 Colombia
away_team_away_team_gender                                                   male
away_team_away_team_group                                                    None
away_team_country_id                                                           49
away_team_country_name                                                   Colombia
away_team_managers              [{'id': 5905, 'name': 'Néstor Gabriel Lorenzo'...
metadata_data_version                                                       1.1.0
metadata_shot_fidelity_version                                                  2
metadata_xy_fidelity_version                                                    2
competition_stage_id                                                           26
competition_stage_name                                                      Final
stadium_id                                                                   5337
stadium_name                                                    Hard Rock Stadium
stadium_country_id                                                            241
stadium_country_name                                     United States of America
referee_id                                                                   2638
referee_name                                                        Raphael Claus
referee_country_id                                                             31
referee_country_name                                                       Brazil
Name: 0, dtype: object
```

Next, we'll load one of the events JSON files and also add a column called `match_id` to that DataFrame:

```
response = requests.get(
  "https://raw.githubusercontent.com/statsbomb/open-data/master/data/events/3943077.json"
)
events_df = pd.json_normalize(response.json(), sep='_')
events_df["match_id"] = 3943077
```

And again, let's have a look at the first row:

```
with pd.option_context("display.max_rows", None):
    first_row = events_df.iloc[0]
    non_nan_columns = first_row[first_row.notna()].T
    display(non_nan_columns)
```

```
id                                   279b7d66-92b5-4daa-8ff6-cba8fce271d9
index                                                                   1
period                                                                  1
timestamp                                                    00:00:00.000
minute                                                                  0
second                                                                  0
possession                                                              1
duration                                                              0.0
type_id                                                                35
type_name                                                     Starting XI
possession_team_id                                                    779
possession_team_name                                            Argentina
play_pattern_id                                                         1
play_pattern_name                                            Regular Play
team_id                                                               779
team_name                                                       Argentina
tactics_formation                                                   442.0
tactics_lineup          [{'player': {'id': 6909, 'name': 'Damián Emili...
match_id                                                          3943077
Name: 0, dtype: object
```

## Querying Pandas DataFrames[​](#querying-pandas-dataframes "Direct link to Querying Pandas DataFrames")

Next, let's see how to query these DataFrames using chDB.
We'll import the library:

```
import chdb
```

We can query Pandas DataFrames by using the `Python` table function:

```
SELECT *
FROM Python(<name-of-variable>)
```

So, if we wanted to list the columns in `matches_df`, we could write the following:

```
chdb.query("""
DESCRIBE Python(matches_df)
SETTINGS describe_compact_output=1
""", "DataFrame")
```