Selecting columns by name in Pandas - Time & Space Complexity
When we select columns by their names in pandas, we want to know how the time it takes changes as the data grows.
We ask: How does the work increase when the number of rows or columns grows?
Analyze the time complexity of the following code snippet.
import pandas as pd
df = pd.DataFrame({
'A': range(1000),
'B': range(1000, 2000),
'C': range(2000, 3000)
})
selected = df[['A', 'C']]
This code creates a DataFrame with 3 columns and selects two columns by their names.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing and copying the selected columns from the DataFrame.
- How many times: Once per row for each selected column.
As the number of rows grows, the work to copy selected columns grows proportionally.
| Input Size (n rows) | Approx. Operations |
|---|---|
| 10 | 20 (2 columns x 10 rows) |
| 100 | 200 (2 columns x 100 rows) |
| 1000 | 2000 (2 columns x 1000 rows) |
Pattern observation: The operations grow linearly with the number of rows.
Time Complexity: O(n)
This means the time to select columns grows directly with the number of rows in the DataFrame.
[X] Wrong: "Selecting columns by name is instant and does not depend on data size."
[OK] Correct: Even though column names are used, pandas must copy data for each row in those columns, so time grows with rows.
Understanding how data selection scales helps you write efficient code and explain your choices clearly in real projects.
"What if we select all columns instead of just a few? How would the time complexity change?"
Practice
Which syntax correctly selects a single column named 'Age' from a pandas DataFrame df?
Solution
Step 1: Understand single column selection syntax
Using single brackets with the column name as a string selects one column and returns a Series.Step 2: Compare options
df['Age']is the correct syntax. Double brackets return a DataFrame, and other options are invalid syntax.Final Answer:
df['Age'] -> Option BQuick Check:
Single brackets select one column [OK]
- Using double brackets for single column returns DataFrame, not Series
- Trying to use parentheses instead of brackets
- Using dot notation with brackets incorrectly
Which of the following is the correct syntax to select multiple columns 'Name' and 'Salary' from a DataFrame df?
Solution
Step 1: Recall syntax for selecting multiple columns
Multiple columns must be passed as a list inside double brackets.Step 2: Evaluate options
Onlydf[['Name', 'Salary']]correctly uses a list inside double brackets. Others are invalid or return separate Series.Final Answer:
df[['Name', 'Salary']] -> Option AQuick Check:
Double brackets + list select multiple columns [OK]
- Using single brackets with multiple column names separated by comma
- Using bitwise AND (&) instead of list
- Trying to select columns separately without combining
Given the DataFrame df below, what is the output of df[['City', 'Age']]?
import pandas as pd
data = {'Name': ['Alice', 'Bob'], 'Age': [25, 30], 'City': ['NY', 'LA']}
df = pd.DataFrame(data)Solution
Step 1: Understand the selection
The code selects columns 'City' and 'Age' in that order from the DataFrame.Step 2: Check the output order and values
The output DataFrame has columns 'City' then 'Age' with values from original data.Final Answer:
City Age 0 NY 25 1 LA 30 -> Option CQuick Check:
Selected columns match output order [OK]
- Mixing column order in output
- Selecting wrong columns
- Confusing Series output with DataFrame output
What is wrong with this code to select columns 'A' and 'B' from DataFrame df?
df['A', 'B']
Solution
Step 1: Analyze the indexing syntax
Using single brackets with multiple comma-separated strings creates a tuple key, not a list.Step 2: Understand pandas column selection behavior
Since ('A', 'B') is a tuple, pandas looks for a column with that exact tuple name, which does not exist, causing KeyError.Final Answer:
It raises a KeyError because tuple ('A', 'B') is not a column -> Option AQuick Check:
Single brackets with comma create tuple key, causing KeyError [OK]
- Using single brackets with multiple column names separated by comma
- Expecting a DataFrame but getting error
- Confusing tuple with list in indexing
You have a DataFrame df with columns 'Name', 'Age', 'Salary', and 'Department'. You want to create a new DataFrame with only 'Name' and 'Salary', but also want to rename 'Salary' to 'Income'. Which code correctly does this?
Solution
Step 1: Select columns 'Name' and 'Salary'
Use double brackets with a list:df[['Name', 'Salary']]to get a DataFrame with those columns.Step 2: Rename 'Salary' to 'Income'
Userename(columns={'Salary': 'Income'})to rename the column correctly.Final Answer:
new_df = df[['Name', 'Salary']].rename(columns={'Salary': 'Income'}) -> Option DQuick Check:
Select columns then rename with columns dict [OK]
- Using single brackets for multiple columns
- Passing rename argument without 'columns=' keyword
- Passing list instead of dict to rename
Ready to prove it?
Pass the test and this pattern moves unknown
