Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 64 ยท Selecting and Filtering

Selecting Columns and Rows

Pick columns with df["col"] and df[["a", "b"]], slice rows, preview with head and sample, reorder and drop columns, and avoid the attribute-access traps.

Once your data is in a DataFrame, the first thing you do with it is look at part of it: one column, a handful of columns, the first few rows. pandas gives you a few short, readable ways to do that with plain square brackets. They look simple, but each form returns a different kind of object, and knowing which is which saves a lot of confusion later.

This lesson covers the everyday tools: selecting columns by name, slicing rows, previewing with head and sample, and reordering or dropping columns. The more precise tools, loc and iloc, come in the next lesson. If you need a reminder of what a DataFrame is, see DataFrame Basics.

One column gives a Series

Put a column name in square brackets and you get a Series: a single labeled column with the same index as the DataFrame.

Python Selecting a single column

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The result keeps the column name as the Series name and shares the row labels with the DataFrame. You can chain methods straight onto it, as in books["year"].max().

Several columns give a DataFrame

Pass a list of names and you get a smaller DataFrame, with the columns in the order you listed them. Note the double brackets: the outer pair is the selection, the inner pair is the Python list.

Python Selecting several columns

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The difference between books["title"] and books[["title"]] matters. The first is one-dimensional (a Series); the second is two-dimensional (a DataFrame with one column). Some functions accept only one of them, so check with type() or .shape when something complains.

flowchart LR
    DF["DataFrame<br/>4 columns, 5 rows"] -->|"df['year']"| S["Series<br/>1 column"]
    DF -->|"df[['title', 'year']]"| D2["DataFrame<br/>2 columns"]
    DF -->|"df[1:4]"| D3["DataFrame<br/>3 rows"]

A string gives a Series, a list of strings gives a DataFrame, and a slice gives a DataFrame of rows.

A missing name raises KeyError. That is useful: a typo in a column name fails loudly instead of silently returning nothing.

Attribute access and why to avoid it

Columns can also be read as attributes: books.title works when the column name is a valid Python identifier. It is shorter, and you will see it in many examples. It is also fragile.

Python Attribute access traps

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Three problems show up here:

  • A column named like an existing attribute or method (count, shape, size, sum, index, T) returns the attribute, not your data.
  • Names with spaces, dashes or leading digits cannot be written as attributes.
  • Assigning with df.total = ... sets a plain Python attribute on the object and does not add a column (recent versions warn about it). Use df["total"] = ... instead.

The rule is simple: use df["name"] in anything you keep, and treat df.name as a convenience for throwaway exploration.

Slicing rows with brackets

A slice inside square brackets selects rows by position, exactly like slicing a Python list: the start is included and the stop is excluded.

Python Slicing rows

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The last example is a classic surprise. A slice of integers is positional (end excluded), while a slice of labels includes the end label. To avoid mixing the two ideas, the next lesson introduces iloc for positions and loc for labels, and most careful code uses those instead of bare slices.

A bare df[...] does three different jobs depending on what you put inside: a string or list selects columns, a slice selects rows, and a boolean Series filters rows (see Filtering with Conditions). It is convenient, but it is the reason loc and iloc exist.

Previewing: head, tail and sample

For large tables you rarely print everything. Three methods show a piece:

  • head(n) returns the first n rows (default 5).
  • tail(n) returns the last n rows. A negative n means "all but".
  • sample(n) returns n random rows. Pass random_state for a repeatable result, or frac to take a fraction.
Python head, tail and sample

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that sample keeps the original row labels, so you can still see which rows were drawn. Without random_state the result changes on every run, which is fine for exploring but not for tests or lessons.

Reordering columns

A new column order is just a selection with the names in the order you want. You can type the list, or build it from df.columns:

Python Reordering columns

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

None of these change books; each produces a new DataFrame. To keep the new order, assign the result to a variable.

Dropping columns

drop removes columns you do not need. Name them with columns=, which is clearer than the axis argument.

Python drop and the opposite, filter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

drop returns a new DataFrame; the original stays as it was. With Copy-on-Write, which is always on in pandas 3, that new object behaves as an independent copy when you change it, so you never need to worry about a hidden link back to the original. You will see this in detail in Creating and Changing Columns. Avoid inplace=True: it gives no real speed gain and makes code harder to chain.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does df[['price']] return?

    A list of names always returns a DataFrame, even when the list has one item. A single string, df['price'], returns a Series.
  2. 2. A DataFrame has a column called size. What does df.size return?

    size is an existing DataFrame attribute, so attribute access finds it first. Use df['size'] to read the column.
  3. 3. With the default integer index, what does df[1:3] return?

    A slice in brackets selects rows by position, and the stop position is excluded, as with Python lists.
  4. 4. How do you get a repeatable random sample of 5 rows?

    sample(n) picks n random rows, and random_state fixes the random generator so the same rows are drawn each time.

Summary

  • df["col"] returns a Series; df[["a", "b"]] returns a DataFrame with the columns in the order you list them.
  • Prefer df["col"] over df.col: attribute access fails for names that clash with methods or contain spaces, and cannot create columns.
  • A slice in brackets selects rows by position (stop excluded); a slice of labels includes the end label.
  • head, tail and sample preview a table; set random_state for repeatable samples.
  • Reorder columns by selecting them in the order you want; drop(columns=...) removes them and returns a new DataFrame.

Next up: choose rows and columns with labels and positions in loc and iloc.