| 1 | # vega_datasets |
| 2 | |
| 3 | [](https://travis-ci.org/altair-viz/vega_datasets) |
| 4 | [](https://github.com/altair-viz/vega_datasets/actions?query=workflow%3Abuild) |
| 5 | [](https://github.com/altair-viz/vega_datasets/actions?query=workflow%3Alint) |
| 6 | [](https://github.com/psf/black) |
| 7 | |
| 8 | A Python package for offline access to [vega datasets](https://github.com/vega/vega-datasets). |
| 9 | |
| 10 | This package has several goals: |
| 11 | |
| 12 | - Provide straightforward access in Python to the datasets made available at [vega-datasets](https://github.com/vega/vega-datasets). |
| 13 | - return the results in the form of a Pandas dataframe. |
| 14 | - wherever dataset size and/or license constraints make it possible, bundle the dataset with the package so that datasets can be loaded in the absence of a web connection. |
| 15 | |
| 16 | Currently the package bundles a half-dozen datasets, and falls back to using HTTP requests for the others. |
| 17 | |
| 18 | ## Installation |
| 19 | ``vega_datasets`` is compatible with Python 3.5 or newer. Install with: |
| 20 | ``` |
| 21 | $ pip install vega_datasets |
| 22 | ``` |
| 23 | |
| 24 | ## Usage |
| 25 | |
| 26 | The main object in this library is ``data``: |
| 27 | |
| 28 | ```python |
| 29 | >>> from vega_datasets import data |
| 30 | ``` |
| 31 | |
| 32 | It contains attributes that access all available datasets, locally if |
| 33 | available. For example, here is the well-known iris dataset: |
| 34 | |
| 35 | ```python |
| 36 | >>> df = data.iris() |
| 37 | >>> df.head() |
| 38 | petalLength petalWidth sepalLength sepalWidth species |
| 39 | 0 1.4 0.2 5.1 3.5 setosa |
| 40 | 1 1.4 0.2 4.9 3.0 setosa |
| 41 | 2 1.3 0.2 4.7 3.2 setosa |
| 42 | 3 1.5 0.2 4.6 3.1 setosa |
| 43 | 4 1.4 0.2 5.0 3.6 setosa |
| 44 | ``` |
| 45 | |
| 46 | If you're curious about the source data, you can access the URL for any of the available datasets: |
| 47 | |
| 48 | ```python |
| 49 | >>> data.iris.url |
| 50 | 'https://cdn.jsdelivr.net/npm/[email protected]/data/iris.json' |
| 51 | ``` |
| 52 | |
| 53 | For datasets bundled with the package, you can also find their location on disk: |
| 54 | |
| 55 | ```python |
| 56 | >>> data.iris.filepath |
| 57 | '/lib/python3.6/site-packages/vega_datasets/data/iris.json' |
| 58 | ``` |
| 59 | |
| 60 | ## Available Datasets |
| 61 | |
| 62 | To list all the available datsets, use ``list_datasets``: |
| 63 | |
| 64 | ```python |
| 65 | >>> data.list_datasets() |
| 66 | ['7zip', 'airports', 'anscombe', 'barley', 'birdstrikes', 'budget', 'budgets', 'burtin', 'cars', 'climate', 'co2-concentration', 'countries', 'crimea', 'disasters', 'driving', 'earthquakes', 'ffox', 'flare', 'flare-dependencies', 'flights-10k', 'flights-200k', 'flights-20k', 'flights-2k', 'flights-3m', 'flights-5k', 'flights-airport', 'gapminder', 'gapminder-health-income', 'gimp', 'github', 'graticule', 'income', 'iris', 'jobs', 'londonBoroughs', 'londonCentroids', 'londonTubeLines', 'lookup_groups', 'lookup_people', 'miserables', 'monarchs', 'movies', 'normal-2d', 'obesity', 'points', 'population', 'population_engineers_hurricanes', 'seattle-temps', 'seattle-weather', 'sf-temps', 'sp500', 'stocks', 'udistrict', 'unemployment', 'unemployment-across-industries', 'us-10m', 'us-employment', 'us-state-capitals', 'weather', 'weball26', 'wheat', 'world-110m', 'zipcodes'] |
| 67 | ``` |
| 68 | |
| 69 | To list local datasets (i.e. those that are bundled with the package and can be used without a web connection), use the ``local_data`` object instead: |
| 70 | |
| 71 | ```python |
| 72 | >>> from vega_datasets import local_data |
| 73 | >>> local_data.list_datasets() |
| 74 | |
| 75 | ['airports', 'anscombe', 'barley', 'burtin', 'cars', 'crimea', 'driving', 'iowa-electricity', 'iris', 'seattle-temps', 'seattle-weather', 'sf-temps', 'stocks', 'us-employment', "wheat"] |
| 76 | ``` |
| 77 | |
| 78 | We plan to add more local datasets in the future, subject to size and licensing constraints. See the [local datasets issue](https://github.com/altair-viz/vega_datasets/issues/1) if you would like to help with this. |
| 79 | |
| 80 | ## Dataset Information |
| 81 | |
| 82 | If you want more information about any dataset, you can use the ``description`` property: |
| 83 | |
| 84 | ```python |
| 85 | >>> data.iris.description |
| 86 | 'This classic dataset contains lengths and widths of petals and sepals for 150 iris flowers, drawn from three species. It was introduced by R.A. Fisher in 1936 [1]_.' |
| 87 | ``` |
| 88 | |
| 89 | This information is also part of the ``data.iris`` doc string. |
| 90 | Descriptions are not yet included for all the datasets in the package; we hope to add more information on this in the future. |