0
0
Data Analysis Pythondata~20 mins

Selecting columns in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Column Selector Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of selecting multiple columns from a DataFrame
What is the output of this code snippet when selecting columns 'A' and 'C' from the DataFrame?
Data Analysis Python
import pandas as pd

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

result = df[['A', 'C']]
print(result)
A
   B  C
0  4  7
1  5  8
2  6  9
B
   A  C
0  1  7
1  2  8
2  3  9
C
   A  B
0  1  4
1  2  5
2  3  6
DKeyError: "['A', 'C'] not found in axis"
Attempts:
2 left
💡 Hint
Remember to use double brackets to select multiple columns in pandas.
data_output
intermediate
1:30remaining
Number of columns after selection
How many columns does the resulting DataFrame have after selecting columns 'X' and 'Y' from this DataFrame?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({
    'X': [10, 20],
    'Y': [30, 40],
    'Z': [50, 60]
})

selected = df[['X', 'Y']]
print(len(selected.columns))
AKeyError
B1
C2
D3
Attempts:
2 left
💡 Hint
Count the columns you selected explicitly.
🔧 Debug
advanced
2:00remaining
Identify the error when selecting columns
What error does this code raise when trying to select columns 'A' and 'D' from the DataFrame?
Data Analysis Python
import pandas as pd

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

result = df[['A', 'D']]
print(result)
AAttributeError: 'DataFrame' object has no attribute 'D'
BIndexError: single positional indexer is out-of-bounds
CTypeError: unhashable type: 'list'
DKeyError: "['A', 'D'] not found in axis"
Attempts:
2 left
💡 Hint
Check if all column names exist in the DataFrame.
🚀 Application
advanced
2:30remaining
Selecting columns based on data type
Given a DataFrame with mixed data types, which code snippet correctly selects only the numeric columns?
Data Analysis Python
import pandas as pd
import numpy as np

df = pd.DataFrame({
    'num1': [1, 2, 3],
    'num2': [4.5, 5.5, 6.5],
    'text': ['a', 'b', 'c'],
    'bool': [True, False, True]
})
Anumeric_df = df.select_dtypes(include=['number'])
Bnumeric_df = df[['num1', 'num2']]
Cnumeric_df = df.select_dtypes(include=['object'])
Dnumeric_df = df.loc[:, df.dtypes == 'bool']
Attempts:
2 left
💡 Hint
Use pandas method to select columns by data type.
🧠 Conceptual
expert
3:00remaining
Effect of chained indexing on column selection
What is the main risk of using chained indexing like df['A']['B'] when selecting columns in pandas?
AIt may return a copy instead of a view, leading to unexpected behavior when modifying data.
BIt always raises a KeyError because pandas does not allow chained indexing.
CIt improves performance by reducing memory usage.
DIt automatically converts columns to numeric types.
Attempts:
2 left
💡 Hint
Think about how pandas handles views and copies.