{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Aggregations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Import the LArray library:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from larray import *" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Load the `population` array and related axes from the `demography_eurostat` dataset:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# load the 'demography_eurostat' dataset\n", "demography_eurostat = load_example_data('demography_eurostat')\n", "\n", "# extract the 'country', 'gender' and 'time' axes\n", "country = demography_eurostat.country\n", "gender = demography_eurostat.gender\n", "time = demography_eurostat.time\n", "\n", "# extract the 'population_5_countries' array as 'population'\n", "population = demography_eurostat.population_5_countries\n", "\n", "# show the 'population' array\n", "population" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The LArray library provides many aggregation functions. The list is given in the [Aggregation Functions](../api.rst#aggregation-functions) subsection of the [API Reference](../api.rst) page.\n", "\n", "Aggregation operations can be performed on axes or groups. Axes and groups can be mixed. \n", "\n", "The main rules are: \n", "\n", "- Axes are separated by commas ``,``\n", "- Groups belonging to the same axis are grouped inside parentheses ()\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Calculate the sum along an axis:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "population.sum(gender)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "or several axes (axes are separated by commas ``,``):" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "population.sum(country, gender)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Calculate the sum along all axes except one by appending `_by` to the aggregation function:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "population.sum_by(time)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Calculate the sum along groups (the groups belonging to the same axis must grouped inside parentheses ()):\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "benelux = population.country['Belgium', 'Netherlands', 'Luxembourg'] >> 'benelux'\n", "fr_de = population.country['France', 'Germany'] >> 'FR+DE'\n", "\n", "population.sum((benelux, fr_de))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Mixing axes and groups in aggregations:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "population.sum(gender, (benelux, fr_de))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "