0PricingLogin
Pandas & NumPy Academy · Lesson

Selecting Columns and Rows

Select single or multiple columns with bracket notation, and retrieve rows by label and position using .loc and .iloc.

Selecting a Single Column

Access a single column by name using bracket notation df['col'], which returns a Series. You can also use dot notation df.col when the column name is a valid Python identifier with no spaces. Bracket notation is always safe; dot notation fails for names that clash with DataFrame methods like count or mean.

import pandas as pd

df = pd.DataFrame({'name': ['Alice', 'Bob'], 'score': [90, 75]})
print(df['name'])     # Series
print(type(df['name']))  # pandas.core.series.Series

Selecting Multiple Columns

To select multiple columns, pass a list of column names inside the brackets: df[['col1', 'col2']]. Note the double brackets — the outer pair is the indexing operator, the inner pair creates a Python list. The result is a DataFrame, not a Series. This is commonly used to extract a feature matrix for machine learning.

import pandas as pd

df = pd.DataFrame({'a': [1,2], 'b': [3,4], 'c': [5,6]})
subset = df[['a', 'c']]
print(type(subset))      # pandas.core.frame.DataFrame
print(subset)

All lessons in this course

  1. Creating DataFrames
  2. Selecting Columns and Rows
  3. Adding and Dropping Columns
  4. Basic DataFrame Inspection
← Back to Pandas & NumPy Academy