Skip to main content
Engineering LibreTexts

17.1: Accessing Individual Rows and Columns

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

    Suppose you have a DataFrame called df. Here’s how you can extract particular rows and columns:

    • df.loc[i] – access the row with index i
    • df.iloc[n] – access row number n
    • df[c] – access column c

    The second of these is reminiscent of the .iloc syntax we learned for Serieses on p. 112. With it, we specify the number we want, rather than the index/key/label. That’s not super common to do, but it happens. More common is the first form: we specify the row we want by its index.

    The last one is tricky, because everyone (including me, several times a week, it seems) assumes that just typing (say) “df[’Bart’]” would give you Bart’s row. This is probably how it ought to work, since Serieses worked that way. Alas, no: if you specify neither .loc nor .iloc, you’re asking for a column, not a row.

    Yet another odd thing is how a single row is presented on the screen. Let’s go back to the simpsons data set (bottom of p. 174), and access the Bart row the proper way (with .loc):

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

    print(simpsons.loc['Bart'])

    | species human

    | age 10

    | gender M

    | fave skateboard

    | IQ 90

    | hair buzz

    | salary 0

    | Name: Bart, dtype: object

    This bugs the heck out of me. Bart, like all other Simpsons, was a row in the original DataFrame, but here, it presents Bart’s information vertically instead of horizontally. I find it visually jarring. The reason Pandas does it this way is that each row of a DataFrame is a Series, and the way Pandas displays Serieses is vertically. We’ll deal somehow.

    Btw, for any of the three options, you can provide a list with multiple things you want, instead of just one thing. You do so by using double boxies:

    • df.loc[[i1,i2,i3,. . . ]] – access the rows with indices i1, i2, i3, etc.
    • df.iloc[[n1,n2,n3,. . . ]] – access the rows numbered n1, n2, n3, etc.
    • df[[c1,c2,c3,. . . ]] – access the columns names c1, c2, c3, etc.

    Examples

    To test your understanding of all of the above, confirm that you understand the following examples:

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

    print(simpsons.iloc[3])

    | species human

    | age 8

    | gender F

    | fave saxophone

    | IQ 200

    | hair curly

    | salary 0

    | Name: Lisa, dtype: object

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

    print(simpsons['age'])

    | name

    | Homer 36

    | Marge 34

    | Bart 10

    | Lisa 8

    | Maggie 1

    | SLH 4

    | Name: age, dtype: int64

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

    print(simpsons.loc)

    | species age gender fave IQ hair salary

    | name

    | Lisa human 8 F saxophone 200.0 curly 0.0

    | Maggie human 1 F pacifier 100.0 curly 0.0

    | Bart human 10 M skateboard 90.0 buzz 0.0

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

    print(simpsons.iloc)

    | species age gender fave IQ hair salary

    | name

    | Marge human 34 F helping others 200.0 stacked tall 0.0

    | Lisa human 8 F saxophone 100.0 curly 0.0

    | Maggie human 1 F pacifier 90.0 curly 0.0

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

    print(simpsons)

    | age fave IQ

    | name

    | Homer 36 beer 74.0

    | Marge 34 helping others 120.0

    | Bart 10 skateboard 90.0

    | Lisa 8 saxophone 200.0

    | Maggie 1 pacifier 100.0

    | SLH 4 NaN 30.0

    Incidentally, you’ll notice how the name values are treated differently from all the other columns, since name is the DataFrame’s index. For one thing, name always appears, even though it’s not included among the columns we asked for. For another, it’s listed at the bottom of the single-row Series listings rather than up with the other values in that row.


    This page titled 17.1: Accessing Individual Rows and Columns 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.