Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Choose a pandas selector by asking two questions: are you identifying rows and columns by labels or by integer positions, and do you need one value or a larger selection? Use [] for a single column or a simple row filter, .loc for label-based selections, .iloc for zero-based positional selections, and .at or .iat for a single scalar.

How do I select a subset of a DataFrame?

Start with this small DataFrame. Its index labels are strings, so the distinction between a label and a row position is visible:

import pandas as pd

df = pd.DataFrame(
    {
        "name": ["Ada", "Linus", "Grace"],
        "age": [36, 34, 40],
        "city": ["London", "Helsinki", "New York"],
    },
    index=["row_a", "row_b", "row_c"],
)

For example, row_a is the label of the first row, while 0 is its zero-based position. The core distinction is:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • .loc uses index and column labels.
  • .iloc uses integer positions, counted from zero.
  • .at and .iat use the same label-versus-position distinction for one value.

Use [] for a column or a straightforward row filter

Select a column

Pass the column label in brackets to get a Series:

df["name"]

This returns the name values for all rows. To select more than one named column, pass a list of labels; the result is a DataFrame:

df[["name", "age"]]

Filter rows with a condition

A boolean condition inside brackets keeps rows where the condition is true:

df[df["age"] > 35]

The condition produces a Boolean Series aligned with the DataFrame’s rows. You can also use an explicit label-based selection when you want to state the row condition and chosen columns together:

df.loc[df["age"] > 35, "name"]

This returns the names of people older than 35. The pandas getting-started tutorial demonstrates this pattern with its Titanic data, selecting names where Age is greater than 35 (pandas getting-started tutorial).

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use .loc when selectors are labels

The general form is df.loc[row_selector, column_selector]. The selector before the comma addresses rows; the selector after it addresses columns. Use : when you want all rows or all columns on one axis.

# Rows by index labels, and columns by names
df.loc["row_a", "name"]

# All rows, selected columns
df.loc[:, ["name", "age"]]

# A range of row labels and two columns
df.loc["row_a":"row_c", ["name", "age"]]

Unlike ordinary Python slicing, a .loc label slice includes its stop label when that label is present. Thus "row_c" is included in the last example. A number passed to .loc is still interpreted as an index label, not as a row number. If a requested label does not exist, .loc raises KeyError.

Use .iloc when selectors are integer positions

The form is df.iloc[row_position, column_position]. Positions start at zero: position 0 is the first row or column. For example:

# First row, second column
df.iloc[0, 1]

# First three rows, second and third columns
df.iloc[0:3, [1, 2]]

In the second example, the row slice includes positions 0, 1, and 2, while the selected column positions are 1 and 2. Positional slices follow Python and NumPy rules: the start is included and the stop is excluded. A scalar or list request for a position outside the axis raises IndexError; slice endpoints may extend out of bounds without that error.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use .at and .iat to get or set one value

When the desired result is one scalar rather than a Series or DataFrame, use the scalar accessor that matches your selector:

Accessor Selector meaning Example
.at Row and column labels df.at["row_a", "age"]
.iat Zero-based row and column positions df.iat[0, 1]

Both examples retrieve Ada’s age from this DataFrame. These accessors can also assign a value, for example df.at["row_a", "age"] = 37. Assignment through .at to a missing indexer can enlarge the object in place; take care when an apparent typo could create a new entry. The pandas API reference documents .iat as the integer-position scalar accessor (DataFrame.iat API reference).

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Which selector should you use?

Need Use What to remember
One named column df["column"] Returns a Series.
Rows matching a simple condition df[condition] Boolean filtering keeps matching rows.
A selection by row or column labels df.loc[rows, columns] Label slices include their stop label when present.
A selection by row or column positions df.iloc[rows, columns] Positions start at zero; slice stops are excluded.
One value by labels df.at[row_label, column_label] Scalar label lookup.
One value by positions df.iat[row_position, column_position] Scalar positional lookup.

The [] syntax is convenient for columns and uncomplicated filtering. For production code, the pandas user guide recommends the explicit access methods .at, .iat, .loc, and .iloc; it does not supply a benchmark or a numeric performance advantage for these methods (pandas indexing and selecting data guide). For version-specific details, consult the documentation matching the pandas version installed in your environment; the guide and API references may describe different releases as they are updated.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.