Skip to main content
Geosciences LibreTexts

15.2: Accessing elements

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

    Accessing elements within a DataFrame is a crucial aspect of data manipulation and analysis, and the pandas library offers a robust set of tools for this purpose. The `.iloc` and `.loc` attributes serve as the primary mechanisms for data retrieval. They each have their own set of capabilities, dictated by the type of index you're working with — integer-based for `.iloc` and label-based for `.loc`.

    Let's consider a simple DataFrame that has names as row indices and exam scores as columns:

    data = {'Exam1': [85, 90, 78], 'Exam2': [88, 84, 76]}

    index = ['Alice', 'Bob', 'Charlie']

    df = pd.DataFrame(data, index=index)

    This produces:

    clipboard_eec435ca6f2d9f1764ef5455d0dbb11ee.png

    Using `.iloc`, which relies on integer-based positioning, you could access Alice's Exam1 score (which is 85) with:

    alice_exam1_score = df.iloc[0, 0]

    Here, the first `0` refers to the row (Alice), and the second `0` refers to the column (Exam1).  The value returned is 85.

    Conversely, `.loc` operates with label-based indices. To access the same score using `.loc`, you would use:

    alice_exam1_score = df.loc['Alice', 'Exam1']

    This produces the same thing, 85. 

    The `.iloc` and `.loc` attributes are incredibly flexible. For instance, if you only specify a row, either by its integer index with `.iloc` or its label with `.loc`, pandas will return all columns for that row:

    alice_all_scores = df.loc['Alice']

    alice_all_scores = df.iloc[0]

    Interestingly, the output in this case would be a Series object, where the original DataFrame's column names serve as the Series' index:

    Exam1    85
    Exam2    88
    Name: Alice, dtype: int64

    Both `.iloc` and `.loc` allow slicing and more complex queries, facilitating the retrieval of multiple rows and columns at once. For example, to get the Exam1 scores for both Alice and Bob, you could use:

    alice_bob_exam1 = df.loc[['Alice', 'Bob'], 'Exam1']
    alice_bob_exam1 = df.iloc[0:2, 0]

    Accessing columns in a DataFrame is a routine operation, and pandas offers two primary ways to accomplish this: using square brackets `[]` or the dot notation `.`. The square brackets are incredibly versatile, allowing for both label-based and, occasionally, integer-based indexing, making them compatible with a wide array of DataFrame configurations. For instance, if your DataFrame `df` has a column named 'Exam1', you could access this column by executing `df['Exam1']`. This method works regardless of whether the column name includes spaces or other special characters. Moreover, square brackets facilitate the selection of multiple columns. You can pass a list of column names like `df` to retrieve multiple columns, which will be returned as a DataFrame.

    In contrast, the dot notation provides a quicker but somewhat restricted way to access DataFrame columns. Utilizing dot notation involves appending the column name to the DataFrame object as an attribute: `df.Exam1`. While this is certainly more concise and often more readable, there are limitations to be aware of. Firstly, this approach is feasible only when the column name is a valid Python identifier, meaning it doesn't contain spaces, dots, or other special characters. Secondly, dot notation is not applicable for variable column names. For example, if the column name is stored in a variable, you would have to use the square bracket notation. Lastly, dot notation doesn't allow for the same complex querying or selection of multiple columns in the way that square brackets do.


    15.2: Accessing elements is shared under a not declared license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?