Skip to main content
Engineering LibreTexts

11: Associative Arrays in Python (1 of 3)

  • Page ID
    39277
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)

    Our next trick is to represent associative arrays (review section 7.1 if you need to) in Python. To do so, we will use another package, which goes by the adorable name “Pandas”:

    Code \(\PageIndex{1}\) (Python):

    import pandas as pd

    This code should go at the top of your first notebook cell, right under your “import numpy as np” line. The two go hand in hand.

    By the way, just as there were other choices besides NumPy ndarrays to represent ordinary arrays, there are other choices in Python for associative arrays. The native Python dict (“dictionary”) is an obvious candidate. Because this won’t work well when the data gets huge, however, and because using Pandas now will set up our usage of tables nicely in the next few chapters, we’re going to use the Pandas Series data type for our associative arrays.


    11: Associative Arrays in Python (1 of 3) is shared under a CC BY-SA license and was authored, remixed, and/or curated by Stephen Davies (allthemath.org) .

    • Was this article helpful?