Selecting What You Need

Read the columns you want, and stop carrying the ones you do not.

usecols on the way in

A wide export is mostly columns you will never touch. Naming the ones you want keeps the table readable and the memory down.

import pandas as pd

df = pd.read_csv("people.csv", usecols=["name", "score"])
print(list(df.columns))

Renaming as you go

Column names from real systems are often shouty or spaced. rename takes a dictionary of old to new and hands back a new table.

import pandas as pd

df = pd.DataFrame({"Full Name": ["Ada"], "SCORE": [92]})
df = df.rename(columns={"Full Name": "name", "SCORE": "score"})
print(list(df.columns))

Exercise

Try It Yourself

Write tidy_columns(df) that returns a new table whose column names are all lowercase with spaces turned into underscores.

Press Run to see output

Check Your Understanding