📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials Pandas Selecting Data

Selecting Data

5 min read Quiz at the end
Select columns with [], rows with loc/iloc, and filter rows with boolean conditions and query().

Selecting Data

import pandas as pd
df = pd.read_csv('data.csv')

# Select columns
df['name']             # Series
df[['name', 'age']]    # DataFrame

# Select rows
df.loc[0]              # by label
df.loc[0:3]            # rows 0 to 3 (inclusive)
df.iloc[0]             # by position
df.iloc[0:3]           # rows 0,1,2
df.iloc[0:3, 1:3]      # rows 0-2, cols 1-2

# Conditional selection
df[df['age'] > 30]
df[(df['age'] > 25) & (df['score'] > 90)]
df[df['name'].isin(['Alice', 'Bob'])]
df[df['name'].str.contains('Al')]
df[df['score'].between(80, 95)]
Topic Quiz · 2 questions

Test your understanding before moving on

1. What is the difference between df.loc and df.iloc?
💡 df.loc['a':'c'] selects by label; df.iloc[0:3] selects by position.
2. How do you select rows where age > 30?
💡 Boolean indexing df[condition] returns a new DataFrame with only rows where condition is True.