What are other .loc column selectors?

QuestionsQuestions8 SkillsProDec, 19 2025
095

Beyond single labels and slices, .loc is very flexible with how you select columns. Here are the other "selectors" you can use in the column_selector position:

1. A List of Labels

Unlike a slice ('name':'major') which selects everything in between, a list allows you to pick specific, non-adjacent columns.

# Select names and scores, skipping age and major
df.loc[1:3, ['name', 'score']]

2. A Colon : (Select All)

If you want to filter specific rows based on a condition but keep all columns, use a single colon.

# Select rows where age > 21, and keep every column
df.loc[df['age'] > 21, :]

3. Boolean Arrays

You can pass a list of True and False values that matches the number of columns. This is often used with logic.

# Select only columns that start with the letter 'm'
column_mask = [col.startswith('m') for col in df.columns]
df.loc[:, column_mask]

4. A Callable (Function)

You can pass a lambda function that looks at the DataFrame. This is useful for "chaining" operations.

# Select columns where the column name has more than 4 characters
df.loc[:, lambda x: [col for col in x.columns if len(col) > 4]]

Summary Table for .loc[row_selector, column_selector]

Selector Type Example Result
Single Label 'name' Returns a Series (one column)
List of Labels ['name', 'score'] Returns specific columns
Slice 'name':'major' Returns columns in that range (inclusive)
Colon : Returns all columns
Boolean Array [True, False, True, False] Returns columns where value is True

Which of these would be most useful for the data you are currently working with?

0 Comments

no data
Be the first to share your comment!