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}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)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