Skip to main content
Engineering LibreTexts

12.1: Accessing Individual Elements

  • Page ID
    39278
  • \( \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}}\)

    We can use the len() function, which we’ve already learned two uses for, in yet a third way: to ascertain the number of key/value pairs in a series. Using the Figure 11.1.2 example:

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

    print(len(alter_egos))

    | 4

    Accessing the value for a given key uses exactly the same syntax that NumPy arrays used (boxies), except with the key in place of the numeric index:

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

    superhero = alter_egos['Peter']

    print("Pssst...Peter is really {}.".format(superhero))

    | Pssst...Peter is really Spidey.

    This is why it’s important that the keys of an associative array be unique. If we type “alter_egos['Peter'],” we need to get back one well-defined answer, not an ambiguous set of alternatives.1 The values, on the other hand, may very well not be unique.

    To overwrite the value for a key with a new value, just treat it as a variable and go:

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

    alter_egos['Bruce'] = 'Batman'

    print(alter_egos)

    | Bruce Batman

    | Peter Spidey

    | Tony Iron Man

    | Thor Thor

    | dtype: object

    This same syntax works for adding an entirely new key/value pair as well:

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

    alter_egos['Diana'] = 'Wonder Woman'

    print(alter_egos)

    | Bruce Batman

    | Peter Spidey

    | Tony Iron Man

    | Thor Thor

    | Diana Wonder Woman

    | dtype: object

    It’s just like with ordinary variables, if you think about it. Saying “x=5” overwrites the current value of x if there already is an x, otherwise it creates a new variable x with that value.

    Finally, to outright remove a key/value pair, you use the del operator:

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

    del alter_egos['Tony'] ]

    print(alter_egos)

    | Bruce Batman

    | Peter Spidey

    | Thor Thor

    | Diana Wonder Woman

    | dtype: object

    Bye bye, Iron Man.

    Don’t get mad when I tell you that all of the above operations work in place on the Series, which is very different than some of the “return a modified copy” style we’ve seen recently. Hence all of these attempts are wrong:

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

    alter_egos = del alter_egos['Tony'] <--- WRONG!

    alter_egos = alter_egos['Bruce'] = 'Batman' <--- WRONG!

    alter_egos = alter_egos['Diana'] = 'Wonder Woman' <--- WRONG!

    You don’t “change a value and get a new Series”; you just “change it.”

    Accessing by Position

    One slightly weird thing you can do with a Pandas Series is ignore the key (index) altogether and instead use the number of the key/value pair to specify what value you want. This gives me the heebie-jeebies, because as I explained back on p. 57, there really isn’t any meaningful “order” to the key/value pairs of an associative array. In true All Things To All People™ fashion, however, Pandas lets you do this.

    Accessing a Value by Position

    You can ask for the value of (say) “the second” superhero. To do so, you use the bizarrely-named .iloc syntax:

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

    a_hero = alter_egos.iloc[1]

    print(a_hero)

    | Spidey

    This is occasionally useful, so I mention it for completeness. The .iloc numbers start with 0 (not 1) as is true throughout Python.

    Accessing a Key by Position

    Similarly, you can get the key (as opposed to the value) of the key/value pair at a particular position. To ask for the key of “the second” superhero, you use the .index syntax:

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

    a_secret_hero = alter_egos.index[1]

    print(a_secret_hero)

    | Peter


    This page titled 12.1: Accessing Individual Elements 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.