Course contentsShow
← Python
Lesson 925 of 1,06015. Data Science FundamentalsFree lesson

Selecting Subsets of Data Using Column Names

Extract specific columns from a DataFrame to isolate relevant variables for targeted analysis and data manipulation tasks.

In this lesson, you will learn how to isolate specific variables within a pandas DataFrame by selecting subsets of data using their associated column names.

Isolating Relevant Variables

When working with large datasets, you rarely need every single piece of information simultaneously. To streamline your analysis and reduce memory overhead, you can extract only the specific columns required for your current task. Think of a DataFrame like a spreadsheet where each column represents a feature or a variable; by passing a list of these labels to the bracket operator, you create a new view or subset containing only the data you intend to analyze.

Syntax for Column Selection

To select a single column, you pass the label as a string inside the brackets. If you need to select multiple columns, you pass a list of strings.

import pandas as pd

# Creating a sample DataFrame
data = {
    'employee': ['Alice', 'Bob', 'Charlie'],
    'salary': [70000, 80000, 120000],
    'department': ['HR', 'Engineering', 'Engineering']
}
df = pd.DataFrame(data)

# Extracting a single column (returns a Series)
salaries = df['salary']

# Extracting a subset of multiple columns (returns a new DataFrame)
subset = df[['employee', 'salary']]

Why Use Targeted Selection?

Selecting subsets by name is more robust than selecting by position (like index 0 or 1). Because column names are descriptive, your code remains readable even if the order of columns in the source file changes. Furthermore, isolating specific features is a standard practice in data science pipelines to simplify downstream operations, such as feeding specific inputs into a statistical model or performing group-wise aggregations without the noise of irrelevant variables.

Remember that when you select multiple columns using a list, pandas returns a new DataFrame. If you select a single column using a string, you receive a Series. This distinction is important when you plan to perform subsequent vector-based operations, as it affects the shape and dimensions of your object.

Key Takeaway: You can efficiently isolate variables by passing column labels as strings or lists of strings to the DataFrame indexer, ensuring your data manipulation remains targeted, readable, and robust to structural changes.