#!/usr/bin/env python
# coding: utf-8
# This is also available as Jupyter notebook on https://github.com/janbrus/ssb-api-python-examples

# # Basic example - get CSV from Statistics Norway's API to Python pandas
# ## Make simple chart based on a small dataset from [API: readymade datasets](https://data.ssb.no/api/v0/dataset?lang=en)
# ### We use the datasaset  [index on retail sales](https://data.ssb.no/api/v0/dataset/1066?lang=en) as CSV
#

# Import Python pandas in order to make a dataframe
import pandas as pd

# Get content from the API and put it in dataframe 'df'.
# CSV here is is not UTF-8, but ISO-8859-1. In order to get ÆØÅ correct we have to specify the encoding.
df = pd.read_csv("https://data.ssb.no/api/v0/dataset/1066.csv?lang=en", encoding = "ISO-8859-1")

# Shape shows number of rows and columns
df.shape

# The top of the dataset
df.head()

# Optional: convert to datetime, see also other examples on date converting.
#df['month']= pd.to_datetime(df['month'].str.replace('M', '-'))

# Set month column as index.
df.index = df['month']

# info() is a good way to get information on a dataframe.
df.info()

# Make a chart using Pandas plot
df.plot(x='month', y='07129: Index of retail sales, by industry, month and contents', figsize=(12, 6), color='g')

# Alternative: Month and values to a series object
series1 = df['07129: Index of retail sales, by industry, month and contents']


# Basic plot of Pandas Series
series1.plot()
