Package: fetcha
fetchaTalk to SSB using Python.importfetchaasfetchaimportlogging# Turn off INFO-warningslogging.getLogger().setLevel(logging.WARNING)Installation# >> pip install git+https://github.com/dafeda/fetcha.git --upgrade# Instantiate object with specific table_id that refers to a SSB-table.# 10945 refers to Monetary aggregates M1, M2 and M3:# https://www.ssb.no/en/statbank/table/10945ssb_10945=fetcha.SSB("10945",language="en")# Number of rows in table.ssb_10945.nrows_tot()1422# Number of rows per period.ssb_10945.nrows_period()9# Get all available periodsperiods=ssb_10945.periods()periods[-7:]['2020M08', '2020M09', '2020M10', '2020M11', '2020M12', '2021M01', '2021M02']# Fetch latest period.# Returns a pandas dataframe with its index set with verify_integrity set to True.# If the dataframe is lacking an index, it means that the index columns do not make up a unique combination.df_latest=ssb_10945.fetch()df_latest.head()valuecontentsmonthMonetary aggregate M1. Stocks (NOK million)2021M022526071.0Monetary aggregate M2. Stocks (NOK million)2021M022695383.0Monetary aggregate M3. Stocks (NOK million)2021M022697783.0Monetary aggregate M1. Transactions last 12 months (NOK million)2021M02359029.0Monetary aggregate M2. Transactions last 12 months (NOK million)2021M02343687.0# Fetch list of periodsdf_periods=ssb_10945.fetch(["2019M12","2020M01","2020M02"])df_periods.head()valuecontentsmonthMonetary aggregate M1. Stocks (NOK million)2019M122159770.02020M012182450.02020M022175681.0Monetary aggregate M2. Stocks (NOK million)2019M122345545.02020M012364841.0# Fetch whole year of datadf_year=ssb_10945.fetch("2020")df_year.head()valuecontentsmonthMonetary aggregate M1. Stocks (NOK million)2020M012182450.02020M022175681.02020M032300443.02020M042340381.02020M052374607.0# Fetch multiple yearsdf_years=ssb_10945.fetch(["2019","2020"])df_year.head()valuecontentsmonthMonetary aggregate M1. Stocks (NOK million)2020M012182450.02020M022175681.02020M032300443.02020M042340381.02020M052374607.0# Reset index before pivotingdf_year=df_year.reset_index().pivot(index="month",columns="contents")df_year.head()valuecontentsMonetary aggregate M1. 12-month growth (per centMonetary aggregate M1. Stocks (NOK million)Monetary aggregate M1. Transactions last 12 months (NOK million)Monetary aggregate M2. 12-month growth (per cent)Monetary aggregate M2. Stocks (NOK million)Monetary aggregate M2. Transactions last 12 months (NOK million)Monetary aggregate M3. 12-month growth (per cent)Monetary aggregate M3. Stocks (NOK million)Monetary aggregate M3. Transactions last 12 months (NOK million)month2020M013.12182450.066236.03.92364841.087622.03.72368402.084912.02020M023.22175681.066037.03.82360484.086360.03.72364033.083138.02020M037.02300443.0148469.07.52489403.0170692.07.32492801.0167960.02020M049.82340381.0205486.09.52522315.0216155.09.42525731.0214558.02020M0510.92374607.0232311.010.22552508.0234581.010.12555817.0232003.0ssb_10948=fetcha.SSB("10948",language="en")df_10948=ssb_10948.fetch("2020")df_10948.head()valueholding sectorcontentsmonthMoney holding sectorMonetary aggregate M3. Stocks, seasonally adjusted (NOK million)2020M012374459.02020M022387955.02020M032499994.02020M042543868.02020M052580435.0# Fetch and join# Get another table so we have something to join with.ssb_10948=fetcha.SSB("10948",language="en")df_10948=ssb_10948.fetch("2020")df_10948=df_10948.reset_index().pivot_table(index="month",columns="contents",aggfunc="mean")df_10948.join(df_year).head()valuecontents1-month growth, seasonally adjusted (per cent)Monetary aggregate M3. Stocks, seasonally adjusted (NOK million)Transactions last month, seasonally adjusted (NOK million)Monetary aggregate M1. 12-month growth (per centMonetary aggregate M1. Stocks (NOK million)Monetary aggregate M1. Transactions last 12 months (NOK million)Monetary aggregate M2. 12-month growth (per cent)Monetary aggregate M2. Stocks (NOK million)Monetary aggregate M2. Transactions last 12 months (NOK million)Monetary aggregate M3. 12-month growth (per cent)Monetary aggregate M3. Stocks (NOK million)Monetary aggregate M3. Transactions last 12 months (NOK million)month2020M01-10.02949783.6-1329.83.12182450.066236.03.92364841.087622.03.72368402.084912.02020M023.32955182.03182.03.22175681.066037.03.82360484.086360.03.72364033.083138.02020M03541.24999997.438556.47.02300443.0148469.07.52489403.0170692.07.32492801.0167960.02020M0419.361017547.018928.09.82340381.0205486.09.52522315.0216155.09.42525731.0214558.02020M0514.821032174.217398.210.92374607.0232311.010.22552508.0234581.010.12555817.0232003.0# SSB has a limit of 300k rows per transaction.# Some tables have more than that in one period.ssb_10261=fetcha.SSB("10261",language="en")# Gives warning and returns None.df_10261=ssb_10261.fetch()WARNING:fetcha.ssb:Query exceeds SSB limit of 300k rows per transaction. Current query tries to fetch 607104 rows. User a filter# Can pass filter to fetch(), but first we need to choose what we want.# Use variable levels to see which options you have.ssb_10261.levels0    {'code': 'Region', 'text': 'region', 'values':...
1    {'code': 'Kjonn', 'text': 'sex', 'values': ['0...
2    {'code': 'Alder', 'text': 'age', 'values': ['9...
3    {'code': 'Diagnose3', 'text': 'diagnosis: Chap...
4    {'code': 'ContentsCode', 'text': 'contents', '...
5    {'code': 'Tid', 'text': 'year', 'values': ['20...
Name: variables, dtype: object# We limit the region to "The whole country".ssb_10261.levels.iloc[0]{'code': 'Region',
 'text': 'region',
 'values': ['0',
  '30',
  '01',
  '02',
  '03',
  '34',
  '04',
  '05',
  '06',
  '38',
  '07',
  '08',
  '42',
  '09',
  '10',
  '11',
  '46',
  '12',
  '14',
  '15',
  '50',
  '16',
  '17',
  '18',
  '54',
  '19',
  '20',
  'F00',
  '9',
  'H03',
  'H04',
  'H05',
  'H12',
  'Uoppgitt'],
 'valueTexts': ['The whole country',
  'Viken',
  'Østfold (-2019)',
  'Akershus (-2019)',
  'Oslo',
  'Innlandet',
  'Hedmark (-2019)',
  'Oppland (-2019)',
  'Buskerud (-2019)',
  'Vestfold og Telemark',
  'Vestfold (-2019)',
  'Telemark (-2019)',
  'Agder',
  'Aust-Agder (-2019)',
  'Vest-Agder (-2019)',
  'Rogaland',
  'Vestland',
  'Hordaland (-2019)',
  'Sogn og Fjordane (-2019)',
  'Møre og Romsdal',
  'Trøndelag - Trööndelage',
  'Sør-Trøndelag (-2017)',
  'Nord-Trøndelag (-2017)',
  'Nordland',
  'Troms og Finnmark - Romsa ja Finnmárku',
  'Troms - Romsa (-2019)',
  'Finnmark - Finnmárku (-2019)',
  'Total',
  'Uoppgitt',
  'Helseregion Vest',
  'Helseregion Midt-Norge',
  'Helseregion Nord',
  'Helseregion Sør-Øst',
  'Unknown'],
 'elimination': True}fltr=[{"code":"Region","values":["0"]}]df_10261=ssb_10261.fetch(fltr=fltr)df_10261.shape(17856, 1)df_10261.sample(10)valueregionsexagediagnosis: Chapter in ICD-10contentsyearThe whole countryMales40-59 yearsInfluenza and pneumoniaNumber of day cases201976.0Females20-39 yearsInjuries of upper extremitiesPatients with day cases2019613.060-69 yearsCONGENITAL MALFORMATIONSNumber of bed-days2019290.0Both sexes20-39 yearsCardiac dysrhythmiasPatients with day cases2019276.0PREGNANCY, CHILDBIRTH AND THE PUERPERIUMNumber of out-patient consultations2019109637.0Females60-69 yearsGlaucomaNumber of day cases2019253.0Both sexesYears, totalOther maternal disorders predominantly related to pregnancyIn-patients20191478.060-69 yearsDiabetes mellitusIn-patients2019488.070-79 yearsOther diseases of oesophagus, stomach and duodenumNumber of bed-days20193265.00-9 yearsMalignant neoplasms of female genital organsOut-patients20191.0