Selecting Rows & Columns

Python · Pandas Basics

Theory

pandas has a few selectors, each with one job. df['name'] returns one column as a Series and df[['name', 'amount']] returns several as a DataFrame. .loc selects by label: df.loc[row_labels, column_names], where slices include both ends, so df.loc[1:3, ['name', 'amount']] gives rows labelled 1, 2 and 3. .iloc selects by integer position like a Python list, so slices exclude the end: df.iloc[0:2, [0, 2]] gives the first two rows and the first and third columns.

For a single value use .at[label, col] or .iat[pos, pos], which are faster than loc/iloc. .loc also accepts boolean masks, so df.loc[df['city'] == 'Pune', 'amount'] filters rows and picks a column in one step, and the same form works for assignment: df.loc[mask, 'amount'] = 0. set_index('order_id') turns a column into the row labels so .loc can look rows up by business key.

Why we use it

Clear, explicit selection avoids the classic pandas bugs: off-by-one slices, label and position mixed up, and chained indexing that silently updates a copy instead of your DataFrame.

When to use it

Use [] for columns, .loc for labels and masks, .iloc for positions (first n rows, every other column) and .at/.iat for one cell. To update values always use a single .loc[mask, col] = value, not df[mask]['col'] = value.

How it works

  1. What do you want to select?
  2. Only columns? Yes: Use df['col'] or df[['c1', 'c2']]. No: Rows are involved.
  3. By label or condition? Yes: Use df.loc[rows, cols]. No: Use df.iloc[row_pos, col_pos].
  4. Just one cell? Yes: Use .at[label, col] or .iat[i, j]. No: Keep the loc/iloc result.
  5. Selected Series or DataFrame

Syntax

df['col']; df[['c1', 'c2']]
df.loc[row_labels, col_labels]
df.iloc[row_pos, col_pos]
df.at[label, 'col']; df.iat[i, j]

Example

print(df.loc[1:3, ['name', 'amount']])
print(df.iloc[0:2, [0, 2]])
print(df.at[2, 'name'])

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic