0
0
Pandasdata~10 mins

Selecting multiple columns in Pandas - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Selecting multiple columns
Start with DataFrame
Specify list of columns
Use DataFrame[columns_list
Extract selected columns
New DataFrame with selected columns
We start with a DataFrame, specify which columns we want, then extract those columns to get a new DataFrame.
Execution Sample
Pandas
import pandas as pd

df = pd.DataFrame({
    'A': [1,2,3], 'B': [4,5,6], 'C': [7,8,9]
})

selected = df[['A', 'C']]
This code creates a DataFrame and selects columns 'A' and 'C' to form a new DataFrame.
Execution Table
StepActionInputOutput
1Create DataFrame{'A':[1,2,3],'B':[4,5,6],'C':[7,8,9]}DataFrame with columns A, B, C
2Specify columns to select['A', 'C']List of columns to extract
3Select columns from DataFramedf[['A', 'C']]New DataFrame with columns A and C
4Display selected DataFrameselected A C 0 1 7 1 2 8 2 3 9
💡 Selection complete with only specified columns in new DataFrame
Variable Tracker
VariableStartAfter Step 1After Step 3Final
dfundefinedDataFrame with A,B,CDataFrame with A,B,CDataFrame with A,B,C
selectedundefinedundefinedDataFrame with A,CDataFrame with A,C
Key Moments - 2 Insights
Why do we use double brackets df[['A', 'C']] instead of single brackets?
Using double brackets tells pandas to select multiple columns and return a DataFrame. Single brackets like df['A'] return a Series (one column). See execution_table step 3.
What happens if we specify a column name that does not exist?
Pandas will raise a KeyError because it cannot find the column. This is not shown here but important to check column names before selecting.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output after step 3?
AA list of column names
BA DataFrame with columns A and C
CA Series with column A
DAn error message
💡 Hint
Check the 'Output' column in execution_table row for step 3
At which step do we specify which columns to select?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'Action' column in execution_table to find when columns list is given
If we change the list to ['B'], what will the selected DataFrame contain?
AColumns A and C
BAll columns
COnly column B
DAn empty DataFrame
💡 Hint
Refer to how selection works in execution_table step 3 and variable_tracker for 'selected'
Concept Snapshot
Selecting multiple columns in pandas:
Use df[[list_of_columns]] with double brackets.
Returns a new DataFrame with only those columns.
Single brackets select one column as Series.
Check column names to avoid errors.
Full Transcript
We start with a DataFrame containing columns A, B, and C. We want to select multiple columns, for example A and C. We specify these columns as a list ['A', 'C']. Using double brackets df[['A', 'C']] extracts these columns and returns a new DataFrame with only A and C. This is different from single brackets which return a Series. The variable 'selected' holds this new DataFrame. If a column name does not exist, pandas will raise an error. This process is shown step-by-step in the execution table and variable tracker.