All lessons

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.

Key points, meanings and formulas on one page: choose "Save as PDF" in the print box.

Module 2 · about 15 minutes · uses Lesson 1.4. You’ll be able to pick, drop, reorder and rename columns.

Choose the columns you need by name, drop the rest, and rename them so the table reads well.

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

Pythonorders.csv

2. Drop and rename

Pythonorders.csv

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:

Python

3. By type or by pattern

Pythonorders.csv
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:

Start. 8 columns: order_id, date, customer_id, store_id, product_id, qty, unit_price, discount.
Keep four. orders[['date', 'store_id', 'qty', 'unit_price']]
Rename. .rename(columns={'unit_price': 'price'})
Result.
datestore_idqtyprice
2025-01-01S04219.99
2025-01-01S0114.29

4. Your turn

Your turnproducts.csv
Your turnproducts.csv
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):

Pythonorders.csv

5. Practice

  1. Write the code to keep customer_id and segment from customers.
  2. Why does orders.drop(columns=['qty']) on its own seem to do nothing?
  3. How do you get only the text columns?
Answers
  1. customers[['customer_id', 'segment']].
  2. It returns a new DataFrame that wasn’t stored. Use orders = orders.drop(columns=['qty']).
  3. 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

1How do you keep only the date and qty columns?
2How do you rename 'qty' to 'units'?
3After df.drop(columns=['discount']), is the column gone from df?

Sources

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.