> For the complete documentation index, see [llms.txt](https://docs.pacificdata.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.pacificdata.org/dotstat/plugins/python.md).

# Python

Access Pacific data with the most popular data science solution using the pandasdmx library

![](https://3496061366-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MDSpD50SdXxsYnU7P9D%2F-MKHV2uUL2S5HLMigWA6%2F-MKHW2dcdpOXQ0RLrNth%2Fimage.png?alt=media\&token=a881c3d5-b7dd-4584-aa98-d3f40b8fc775)

### Overview

PDH.stat is part of the `sdmx` Python package developed by Paul Natsuo Kishimoto. More information about the package can be found [here](https://sdmx1.readthedocs.io/en/latest/index.html).

### Install sdmx

These steps have been tested with Python 3.7.4 in an Anaconda environment on Windows 10.

Start by installing sdmx. To learn more about the package, see the code [here](https://github.com/khaeru/sdmx).

To install with pip from the command prompt (note: include the '1' at the end of sdmx): `pip install sdmx1`

In a Python project, import the package: `import sdmx`

### Connect to PDH.stat

To see the available sources and find PDH.stat:

```python
import sdmx
sdmx.list_sources()
```

The source abbreviation for PDH.stat is "SPC" (Pacific Community), as shown below.&#x20;

![](https://3496061366-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MDSpD50SdXxsYnU7P9D%2F-MHshEl8HrNo8hnZtCjv%2F-MHsj9axhyXVJrfVNVla%2Fsources.png?alt=media\&token=e70a58fc-8662-4aa9-ad8a-d7b9cdb13ad9)

To connect to PDH.stat and then view its available data flows:

```python
spc = sdmx.Client('SPC')
datasets = sdmx.to_pandas(spc.dataflow().dataflow)
datasets
```

![](https://3496061366-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MDSpD50SdXxsYnU7P9D%2F-MHshEl8HrNo8hnZtCjv%2F-MHsjhcdKvsk9fEyFHKo%2Fdf.png?alt=media\&token=a0353ee1-23c8-4076-9d0c-3d966a53aa49)

To connect to a data flow and convert it into a pandas Multi-index series:

```python
data = spc.data('DF_CPI')
df = sdmx.to_pandas(data)
df
```

![](https://3496061366-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MDSpD50SdXxsYnU7P9D%2F-MHshEl8HrNo8hnZtCjv%2F-MHsk6tCUqI7YWkIyXmy%2Fdata.png?alt=media\&token=23213842-4257-4817-ae78-29af98ac1ac7)

And then to turn the series into a dataframe, reset the index:

```python
df = df.reset_index()
df
```

![](https://3496061366-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MDSpD50SdXxsYnU7P9D%2F-MKO8DQIjhIfgMHFhOV_%2F-MKO8M8IJurDM4XdDL8k%2FScreenshot%202020-10-24%20154653.png?alt=media\&token=26b6b7b8-8ff3-4fec-8165-f9cebe55ae4e)

For an example of how to use the plugin in combination with the API key and parameter settings, see the [time series plot example](/dotstat/api/scode.md#plot-time-series-population-data-using-the-python-plugin-with-pdh-stat-api).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.pacificdata.org/dotstat/plugins/python.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
