Bird
Raised Fist0
Data Analysis Pythondata~10 mins

Selecting columns in Data Analysis Python - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Selecting columns
Start with DataFrame
↓
Choose column(s) by name
↓
Extract column data
↓
Use extracted data for analysis or display
↓
End
Start with a table of data, pick one or more columns by their names, get those columns out, then use them for your work.
Execution Sample
Data Analysis Python
import pandas as pd

df = pd.DataFrame({'A':[1,2], 'B':[3,4], 'C':[5,6]})
col = df['B']
cols = df[['A','C']]
Create a small table and select one column and then two columns from it.
Execution Table
StepCode LineActionResult
1import pandas as pdLoad pandas librarypandas ready
2df = pd.DataFrame({'A':[1,2], 'B':[3,4], 'C':[5,6]})Create DataFrame with columns A,B,Cdf with 3 columns and 2 rows
3col = df['B']Select column 'B'col is Series: [3, 4]
4cols = df[['A','C']]Select columns 'A' and 'C'cols is DataFrame with columns A and C
5-End of selectionVariables col and cols hold selected data
💡 All requested columns selected, execution ends.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
dfundefined{A: [1,2], B: [3,4], C: [5,6]}{A: [1,2], B: [3,4], C: [5,6]}{A: [1,2], B: [3,4], C: [5,6]}{A: [1,2], B: [3,4], C: [5,6]}
colundefinedundefined[3, 4][3, 4][3, 4]
colsundefinedundefinedundefined{A: [1,2], C: [5,6]}{A: [1,2], C: [5,6]}
Key Moments - 3 Insights
Why do we use single brackets for one column but double brackets for multiple columns?
Single brackets select one column and return a Series (see Step 3). Double brackets select multiple columns and return a DataFrame (see Step 4).
What type of data is returned when selecting a single column?
A single column selection returns a pandas Series, which is like a list with labels (Step 3 shows col as Series).
Can we select columns that do not exist in the DataFrame?
No, selecting non-existing columns will cause an error. The code only selects columns 'A', 'B', and 'C' which exist (Step 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'col' after Step 3?
A[1,2]
B[5,6]
C[3,4]
DUndefined
💡 Hint
Check the 'Result' column for Step 3 in the execution_table.
At which step does the variable 'cols' get assigned a value?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look for 'cols' assignment in the 'Code Line' column of execution_table.
If we select df['A'] instead of df['B'], what would 'col' contain after Step 3?
A[1,2]
B[5,6]
C[3,4]
DError
💡 Hint
Refer to the variable_tracker for values of columns in df.
Concept Snapshot
Selecting columns in pandas:
- Use df['col'] for one column (returns Series)
- Use df[['col1','col2']] for multiple columns (returns DataFrame)
- Column names must exist in df
- Result can be used for analysis or display
Full Transcript
We start with a DataFrame that has columns A, B, and C. We select one column 'B' using single brackets, which gives us a Series with values 3 and 4. Then we select two columns 'A' and 'C' using double brackets, which gives us a smaller DataFrame with those columns. Variables 'col' and 'cols' hold these selections. Single bracket returns Series, double brackets return DataFrame. Selecting columns that do not exist causes errors. This process helps us focus on specific data parts for analysis.

Practice

(1/5)
1. Which of the following is the correct way to select a single column named 'Age' from a DataFrame df?
easy
A. df[['Age']]
B. df.select('Age')
C. df.Age()
D. df['Age']

Solution

  1. Step 1: Understand single column selection syntax

    To select one column from a DataFrame, use df['column_name'].
  2. Step 2: Check each option

    df['Age'] uses df['Age'], which is correct. df[['Age']] returns a DataFrame, not a Series. df.Age() uses parentheses incorrectly. df.select('Age') is not a valid pandas method.
  3. Final Answer:

    df['Age'] -> Option D
  4. Quick Check:

    Single column selection = df['Age'] [OK]
Hint: Use single brackets for one column, double for multiple [OK]
Common Mistakes:
  • Using double brackets for single column returns DataFrame, not Series
  • Using dot notation with parentheses like a function
  • Trying non-existent methods like select()
2. Which syntax correctly selects multiple columns 'Name' and 'Salary' from DataFrame df?
easy
A. df['Name', 'Salary']
B. df[['Name', 'Salary']]
C. df.Name, df.Salary
D. df.select(['Name', 'Salary'])

Solution

  1. Step 1: Recall syntax for selecting multiple columns

    Use double brackets with a list of column names: df[['col1', 'col2']].
  2. Step 2: Evaluate options

    df[['Name', 'Salary']] matches the correct syntax. df['Name', 'Salary'] is invalid syntax. df.Name, df.Salary selects columns separately, not together. df.select(['Name', 'Salary']) is not a pandas method.
  3. Final Answer:

    df[['Name', 'Salary']] -> Option B
  4. Quick Check:

    Multiple columns = df[['Name', 'Salary']] [OK]
Hint: Use double brackets with list for multiple columns [OK]
Common Mistakes:
  • Using single brackets with multiple column names separated by commas
  • Trying to select multiple columns with dot notation
  • Using non-existent select() method
3. Given the DataFrame df below:
   Name  Age  Salary
0  John   28   50000
1  Anna   22   60000
2  Mike   32   55000

What is the output of df[['Name', 'Age']]?
medium
A.
   Name  Age
0  John   28
1  Anna   22
2  Mike   32
B.
   Name  Age  Salary
0  John   28   50000
1  Anna   22   60000
2  Mike   32   55000
C.
0    John
1    Anna
2    Mike
Name: Name, dtype: object
D.
28
22
32

Solution

  1. Step 1: Understand what df[['Name', 'Age']] does

    This selects only the columns 'Name' and 'Age' from the DataFrame, returning a smaller DataFrame with those columns.
  2. Step 2: Match output to options

       Name  Age
    0  John   28
    1  Anna   22
    2  Mike   32
    shows only 'Name' and 'Age' columns with all rows, which matches the expected output.
       Name  Age  Salary
    0  John   28   50000
    1  Anna   22   60000
    2  Mike   32   55000
    includes 'Salary' which is not selected.
    0    John
    1    Anna
    2    Mike
    Name: Name, dtype: object
    shows only the 'Name' column as a Series.
    28
    22
    32
    shows only ages as a list, which is incorrect.
  3. Final Answer:

    DataFrame with columns 'Name' and 'Age' only -> Option A
  4. Quick Check:

    Selected columns = 'Name' and 'Age' only [OK]
Hint: Double brackets return DataFrame with selected columns [OK]
Common Mistakes:
  • Expecting single brackets to return multiple columns
  • Confusing Series output with DataFrame output
  • Including unselected columns in output
4. What is wrong with this code to select the column 'Score' from DataFrame df?
df.Score()
medium
A. Parentheses cause an error; dot notation should be without ()
B. Column names cannot have uppercase letters
C. Column name must be in double brackets
D. Dot notation cannot be used to select columns

Solution

  1. Step 1: Understand dot notation for column selection

    Dot notation selects columns as attributes without parentheses, like df.Score.
  2. Step 2: Identify error in code

    Using parentheses () after Score tries to call it like a function, causing an error.
  3. Final Answer:

    Parentheses cause an error; dot notation should be without () -> Option A
  4. Quick Check:

    Dot notation = df.Score without parentheses [OK]
Hint: Dot notation is attribute access, no parentheses [OK]
Common Mistakes:
  • Adding parentheses after column name with dot notation
  • Thinking dot notation works for all column names
  • Using double brackets with dot notation
5. You have a DataFrame df with columns 'A', 'B', 'C', and 'D'. You want to create a new DataFrame df_new that contains only columns 'B' and 'D' but in reverse order ('D' first, then 'B'). Which code correctly does this?
hard
A. df_new = df['D', 'B']
B. df_new = df[['B', 'D']]
C. df_new = df[['D', 'B']]
D. df_new = df[['B', 'D']].reindex(columns=['B', 'D'])

Solution

  1. Step 1: Understand column selection order

    The order of columns in the list inside double brackets determines the order in the new DataFrame.
  2. Step 2: Check each option

    df_new = df[['D', 'B']] selects columns 'D' then 'B', which matches the requirement. df_new = df[['B', 'D']] selects 'B' then 'D', wrong order. df_new = df['D', 'B'] is invalid tuple syntax inside single brackets. df_new = df[['B', 'D']].reindex(columns=['B', 'D']) reindexes to the same order 'B' then 'D', incorrect.
  3. Final Answer:

    df_new = df[['D', 'B']] -> Option C
  4. Quick Check:

    Order columns by list inside double brackets [OK]
Hint: Order columns in list to set output order [OK]
Common Mistakes:
  • Selecting columns in wrong order
  • Using reindex but specifying the original order
  • Using tuple inside single brackets for multiple columns
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard