Interactive online version: Binder badge

Combining arrays

Import the LArray library:

[1]:
from larray import *
[2]:
# load the 'demography_eurostat' dataset
demography_eurostat = load_example_data('demography_eurostat')

# load 'gender' and 'time' axes
gender = demography_eurostat.gender
time = demography_eurostat.time
[3]:
# load the 'population' array from the 'demography_eurostat' dataset
population = demography_eurostat.population

# show 'population' array
population
[3]:
country  gender\time      2013      2014      2015      2016      2017
Belgium         Male   5472856   5493792   5524068   5569264   5589272
Belgium       Female   5665118   5687048   5713206   5741853   5762455
 France         Male  31772665  32045129  32174258  32247386  32318973
 France       Female  33827685  34120851  34283895  34391005  34485148
Germany         Male  39380976  39556923  39835457  40514123  40697118
Germany       Female  41142770  41210540  41362080  41661561  41824535
[4]:
# load the 'population_benelux' array from the 'demography_eurostat' dataset
population_benelux = demography_eurostat.population_benelux

# show 'population_benelux' array
population_benelux
[4]:
    country  gender\time     2013     2014     2015     2016     2017
    Belgium         Male  5472856  5493792  5524068  5569264  5589272
    Belgium       Female  5665118  5687048  5713206  5741853  5762455
 Luxembourg         Male   268412   275117   281972   289193   296641
 Luxembourg       Female   268627   274563   280986   287056   294026
Netherlands         Male  8307339  8334385  8372858  8417135  8475102
Netherlands       Female  8472236  8494904  8527868  8561985  8606405

The LArray library offers several methods and functions to combine arrays:

  • insert: inserts an array in another array along an axis

  • append: adds an array at the end of an axis.

  • prepend: adds an array at the beginning of an axis.

  • extend: extends an array along an axis.

  • stack: combines several arrays along a new axis.

Insert

[5]:
other_countries = zeros((Axis('country=Luxembourg,Netherlands'), gender, time), dtype=int)

# insert new countries before 'France'
population_new_countries = population.insert(other_countries, before='France')
population_new_countries
[5]:
    country  gender\time      2013      2014      2015      2016      2017
    Belgium         Male   5472856   5493792   5524068   5569264   5589272
    Belgium       Female   5665118   5687048   5713206   5741853   5762455
 Luxembourg         Male         0         0         0         0         0
 Luxembourg       Female         0         0         0         0         0
Netherlands         Male         0         0         0         0         0
Netherlands       Female         0         0         0         0         0
     France         Male  31772665  32045129  32174258  32247386  32318973
     France       Female  33827685  34120851  34283895  34391005  34485148
    Germany         Male  39380976  39556923  39835457  40514123  40697118
    Germany       Female  41142770  41210540  41362080  41661561  41824535
[6]:
# insert new countries after 'France'
population_new_countries = population.insert(other_countries, after='France')
population_new_countries
[6]:
    country  gender\time      2013      2014      2015      2016      2017
    Belgium         Male   5472856   5493792   5524068   5569264   5589272
    Belgium       Female   5665118   5687048   5713206   5741853   5762455
     France         Male  31772665  32045129  32174258  32247386  32318973
     France       Female  33827685  34120851  34283895  34391005  34485148
 Luxembourg         Male         0         0         0         0         0
 Luxembourg       Female         0         0         0         0         0
Netherlands         Male         0         0         0         0         0
Netherlands       Female         0         0         0         0         0
    Germany         Male  39380976  39556923  39835457  40514123  40697118
    Germany       Female  41142770  41210540  41362080  41661561  41824535

See insert for more details and examples.

Append

Append one element to an axis of an array:

[7]:
# append data for 'Luxembourg'
population_new = population.append('country', population_benelux['Luxembourg'], 'Luxembourg')
population_new
[7]:
   country  gender\time      2013      2014      2015      2016      2017
   Belgium         Male   5472856   5493792   5524068   5569264   5589272
   Belgium       Female   5665118   5687048   5713206   5741853   5762455
    France         Male  31772665  32045129  32174258  32247386  32318973
    France       Female  33827685  34120851  34283895  34391005  34485148
   Germany         Male  39380976  39556923  39835457  40514123  40697118
   Germany       Female  41142770  41210540  41362080  41661561  41824535
Luxembourg         Male    268412    275117    281972    289193    296641
Luxembourg       Female    268627    274563    280986    287056    294026

The value being appended can have missing (or even extra) axes as long as common axes are compatible:

[8]:
population_lux = stack({'Male': -1, 'Female': 1}, gender)
population_lux
[8]:
gender  Male  Female
          -1       1
[9]:
population_new = population.append('country', population_lux, 'Luxembourg')
population_new
[9]:
   country  gender\time      2013      2014      2015      2016      2017
   Belgium         Male   5472856   5493792   5524068   5569264   5589272
   Belgium       Female   5665118   5687048   5713206   5741853   5762455
    France         Male  31772665  32045129  32174258  32247386  32318973
    France       Female  33827685  34120851  34283895  34391005  34485148
   Germany         Male  39380976  39556923  39835457  40514123  40697118
   Germany       Female  41142770  41210540  41362080  41661561  41824535
Luxembourg         Male        -1        -1        -1        -1        -1
Luxembourg       Female         1         1         1         1         1

The value being appended can also have the axis along which we are appending:

[10]:
population_nelux = population_benelux[['Netherlands', 'Luxembourg']]
population_nelux
[10]:
    country  gender\time     2013     2014     2015     2016     2017
Netherlands         Male  8307339  8334385  8372858  8417135  8475102
Netherlands       Female  8472236  8494904  8527868  8561985  8606405
 Luxembourg         Male   268412   275117   281972   289193   296641
 Luxembourg       Female   268627   274563   280986   287056   294026
[11]:
population_extended = population.append('country', population_nelux)
population_extended
[11]:
    country  gender\time      2013      2014      2015      2016      2017
    Belgium         Male   5472856   5493792   5524068   5569264   5589272
    Belgium       Female   5665118   5687048   5713206   5741853   5762455
     France         Male  31772665  32045129  32174258  32247386  32318973
     France       Female  33827685  34120851  34283895  34391005  34485148
    Germany         Male  39380976  39556923  39835457  40514123  40697118
    Germany       Female  41142770  41210540  41362080  41661561  41824535
Netherlands         Male   8307339   8334385   8372858   8417135   8475102
Netherlands       Female   8472236   8494904   8527868   8561985   8606405
 Luxembourg         Male    268412    275117    281972    289193    296641
 Luxembourg       Female    268627    274563    280986    287056    294026

See append for more details and examples.

Prepend

Prepend one element to an axis of an array:

[12]:
# append data for 'Luxembourg'
population_new = population.prepend('country', population_benelux['Luxembourg'], 'Luxembourg')
population_new
[12]:
   country  gender\time      2013      2014      2015      2016      2017
Luxembourg         Male    268412    275117    281972    289193    296641
Luxembourg       Female    268627    274563    280986    287056    294026
   Belgium         Male   5472856   5493792   5524068   5569264   5589272
   Belgium       Female   5665118   5687048   5713206   5741853   5762455
    France         Male  31772665  32045129  32174258  32247386  32318973
    France       Female  33827685  34120851  34283895  34391005  34485148
   Germany         Male  39380976  39556923  39835457  40514123  40697118
   Germany       Female  41142770  41210540  41362080  41661561  41824535

See prepend for more details and examples.

Stack

Stack several arrays together to create an entirely new dimension

[13]:
# imagine you have loaded data for each country in different arrays
# (e.g. loaded from different Excel sheets)
population_be = population['Belgium']
population_fr = population['France']
population_de = population['Germany']

print(population_be)
print(population_fr)
print(population_de)
gender\time     2013     2014     2015     2016     2017
       Male  5472856  5493792  5524068  5569264  5589272
     Female  5665118  5687048  5713206  5741853  5762455
gender\time      2013      2014      2015      2016      2017
       Male  31772665  32045129  32174258  32247386  32318973
     Female  33827685  34120851  34283895  34391005  34485148
gender\time      2013      2014      2015      2016      2017
       Male  39380976  39556923  39835457  40514123  40697118
     Female  41142770  41210540  41362080  41661561  41824535
[14]:
# create a new array with an extra axis 'country' by stacking the three arrays population_be/fr/de
population_stacked = stack({'Belgium': population_be, 'France': population_fr, 'Germany': population_de}, 'country')
population_stacked
[14]:
gender  time\country  Belgium    France   Germany
  Male          2013  5472856  31772665  39380976
  Male          2014  5493792  32045129  39556923
  Male          2015  5524068  32174258  39835457
  Male          2016  5569264  32247386  40514123
  Male          2017  5589272  32318973  40697118
Female          2013  5665118  33827685  41142770
Female          2014  5687048  34120851  41210540
Female          2015  5713206  34283895  41362080
Female          2016  5741853  34391005  41661561
Female          2017  5762455  34485148  41824535

See stack for more details and examples.