Skip to main content
Engineering LibreTexts

13: Associative Arrays in Python (3 of 3)

  • Page ID
    39291
  • \( \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}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    But wait, there’s more! We can also use methods like .min(), .max(), .idxmin(), and .idxmax() to get the “extremes” of a Series – i.e. the lowest and highest values in a Series, or their keys (indexes). Note that .idxmin() does not give you the lowest key in the Series! Instead, it gives you the key of the lowest value. Study this code snippet and its output to test your understanding of this:

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

    understanding = pd.Series([15,4,13,3,7], index=[4,10,2,12,9])

    print(understanding)

    print("The min is {}.".format(understanding.min()))

    print("The max is {}.".format(understanding.max()))

    print("The idxmin is {}.".format(understanding.idxmin()))

    print("The idxmax is {}.".format(understanding.idxmax()))

    | 4 15

    | 10 4

    | 2 13

    | 12 3

    | 9 7

    | dtype: int64

    | The min is 3.

    | The max is 15.

    | The idxmin is 12.

    | The idxmax is 4.

    The idxmin and idxmax are 12 and 4, respectively, since the smallest value in the series (the 3) has a key of 12, and the largest value (the 15) has a key of 4.

    If we did actually want the lowest (or highest) key, we could use the .index syntax to achieve that:

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

    print("The lowest key: {}.".format(understanding.index.min()))

    print("The highest key: {}.".format(understanding.index.max()))

    | The lowest key: 2.

    | The highest key: 12.

    And remember that “lowest”/“highest” for string data means alphabetical order.


    This page titled 13: Associative Arrays in Python (3 of 3) is shared under a CC BY-SA 4.0 license and was authored, remixed, and/or curated by Stephen Davies (allthemath.org) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?