Module 2: Selecting and filtering · Lesson 2.1 · about 15 min
Selecting columns
Pick one column or several, reorder them, drop the ones you do not need, rename them, and choose columns by type, so every later step works on a smaller, clearer table.
Module 2 · about 15 minutes · uses Lesson 1.4. You’ll be able to pick, drop, reorder and rename columns.
In everyday terms: packing for a trip: take out only what you need, leave the rest, and label the bags so you can find things.
1. One, several, reordered
2. Drop and rename
Methods like drop and rename return a new DataFrame; assigning it back (orders = ...) keeps the change.
Tidy names make code easier: lower case, underscores, no spaces. One line cleans all of them:
3. By type or by pattern
- df[['a', 'b']]
- Keep these columns, in this order.
- drop(columns=[...])
- Remove columns.
- rename(columns={'old': 'new'})
- Change names.
- select_dtypes / filter(like=...)
- Choose by type or by part of the name.
- Physical meaning
- Narrow tables are faster, easier to read and harder to misuse. Selecting columns is deciding which facts the analysis is about.
“rename changes the table.” Like most pandas methods, it returns a new table. Write df = df.rename(…) to keep the result.
Watch it happen, one step at a time:
orders[['date', 'store_id', 'qty', 'unit_price']].rename(columns={'unit_price': 'price'})| date | store_id | qty | price |
|---|---|---|---|
| 2025-01-01 | S04 | 2 | 19.99 |
| 2025-01-01 | S01 | 1 | 4.29 |
4. Your turn
How it works
Principle: df[...] with a string selects a column; with a list it selects columns; with a True/False Series it selects rows (next lessons). The brackets mean different things depending on what you put in them, which is why loc (Lesson 2.2) exists: it’s explicit.
Chaining: methods that return a DataFrame can be chained, one step per line inside brackets, which reads like a recipe (Lesson 8.1):
5. Practice
- Write the code to keep
customer_idandsegmentfromcustomers. - Why does
orders.drop(columns=['qty'])on its own seem to do nothing? - How do you get only the text columns?
Answers
customers[['customer_id', 'segment']].- It returns a new DataFrame that wasn’t stored. Use
orders = orders.drop(columns=['qty']). df.select_dtypes('str')in pandas 3 (or'object'in older versions).
New words: select, drop, rename, select_dtypes, method chaining.
- Double brackets select several columns in the order you list.
- drop(columns=…) removes; rename(columns={…}) renames.
- select_dtypes and filter choose columns by type or name pattern.
- Assign the result back; methods return new tables.
Check yourself
Sources
- pandas documentation, User Guide: Indexing and selecting data https://pandas.pydata.org/docs/user_guide/indexing.html
- McKinney, W. (2022), Python for Data Analysis, 3rd edition, O'Reilly (open access) https://wesmckinney.com/book/
Your ticks stay in this browser. Save them as a file on the course home.
Updated 2026-11-22. All datasets are made up. Python runs in your browser (the first run downloads it once, about 20 MB); nothing you type is sent anywhere.