# Python

![](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](https://docs.pacificdata.org/api/scode#plot-time-series-population-data-using-the-python-plugin-with-pdh-stat-api).
