Skip to main content
Albero Academy

Selecting Columns

Pick one or many columns, and create new ones.

Selecting & Filtering8 min

Selecting the right slice of data is what you do more than anything else in pandas. It starts with columns: one column is a Series, several columns are a DataFrame.

One vs many

single and multiple columns
df["package"] # one column → a Series
df[["name", "package"]] # several columns → a DataFrame (double [[]])
df.package # attribute access (only for valid identifiers)

Creating columns

derive new columns
df["bonus"] = df["package"] * 0.1 # from other columns
df["package_inr"] = df["package"] * 100000 # vectorised
df = df.drop(columns=["bonus"]) # remove a column

Key takeaways

  1. 01df["col"] returns a Series; df[["a","b"]] returns a DataFrame (double brackets).
  2. 02df.col attribute access works for column names that are valid identifiers.
  3. 03Create columns by assignment from vectorised expressions.
  4. 04Remove columns with df.drop(columns=[...]).

Finished this chapter?

Mark it complete to track your progress through Pandas.