.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/datasets/align.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_datasets_align.py: .. _exa-align: Align datasets ============== Shows how to combine information from two datasets describing the same cases, but not necessarily in the same order. .. GENERATED FROM PYTHON SOURCE LINES 10-29 .. code-block:: Python # Author: Christian Brodbeck import random import string from eelbrain import * # Generate a dataset with known sequence ds = Dataset() ds['ascii'] = Factor(string.ascii_lowercase) # Add an index variable to the dataset to later identify the cases ds.index() # Generate two shuffled copies of the dataset (and print them to confirm that # they are shuffled) ds1 = ds[random.sample(range(ds.n_cases), 15)] ds1.head() .. raw:: html
# ascii index
0 b 1
1 g 6
2 d 3
3 e 4
4 p 15
5 h 7
6 s 18
7 z 25
8 o 14
9 y 24


.. GENERATED FROM PYTHON SOURCE LINES 30-34 .. code-block:: Python ds2 = ds[random.sample(range(ds.n_cases), 16)] ds2.head() .. raw:: html
# ascii index
0 w 22
1 i 8
2 b 1
3 c 2
4 r 17
5 d 3
6 q 16
7 h 7
8 v 21
9 n 13


.. GENERATED FROM PYTHON SOURCE LINES 35-40 Align the datasets ------------------ Use the ``"index"`` variable added above to identify cases and align the two datasets .. GENERATED FROM PYTHON SOURCE LINES 40-47 .. code-block:: Python ds1_aligned, ds2_aligned = align(ds1, ds2, 'index') # show the ascii sequences for the two datasets next to each other to # demonstrate that they are aligned ds1_aligned['ascii_ds2'] = ds2_aligned['ascii'] ds1_aligned .. raw:: html
# ascii index ascii_ds2
0 b 1 b
1 d 3 d
2 e 4 e
3 h 7 h
4 s 18 s
5 z 25 z
6 o 14 o
7 i 8 i
8 v 21 v
9 m 12 m


.. _sphx_glr_download_auto_examples_datasets_align.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: align.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: align.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: align.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_