pbPassingBI
/
Working with DataFrames beginner 5 min

Selecting and renaming columns

Picking columns, dropping them, and renaming without breaking anything.

What you'll be able to do
  • Select single and multiple columns
  • Rename and drop columns
  • Explain Series versus DataFrame

Selecting

df['revenue']              # a Series (one column)
df[['revenue', 'region']]  # a DataFrame (double brackets)
One bracket or two

Single brackets give a Series — a single labelled column. Double brackets give a DataFrame with one column. Most methods work on both, but some do not, and the error message rarely says so clearly.

Renaming

df = df.rename(columns={'rev': 'revenue', 'cust': 'customer'})

# clean every column at once
df.columns = df.columns.str.strip().str.lower().str.replace(' ', '_')

That second line is worth running on every messy import. It fixes trailing spaces and mixed case in one go, which otherwise cause KeyError on names that look correct.

Dropping

df = df.drop(columns=['notes', 'internal_id'])
df = df[['order_id', 'revenue', 'region']]   # keep-only, and sets order

Selecting the columns you want is often clearer than dropping the ones you do not, and it fixes the column order at the same time.

Reassign or use inplace

Most pandas methods return a new DataFrame rather than modifying the original:

Does nothing
df.rename(columns={'a': 'b'})
Correct
df = df.rename(columns={'a': 'b'})

Forgetting the assignment is one of the most common early mistakes, and it fails silently — no error, no change.

Key points
  • Single brackets give a Series; double brackets give a DataFrame
  • df.columns.str.strip().str.lower() fixes most messy headers at once
  • Most methods return a new DataFrame — assign the result
Check yourself