Bird
Raised Fist0
Pandasdata~10 mins

Setting columns as MultiIndex in Pandas - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a MultiIndex from two lists and set it as the DataFrame columns.

Pandas
import pandas as pd

arrays = [['A', 'A', 'B', 'B'], ['one', 'two', 'one', 'two']]
index = pd.MultiIndex.from_arrays(arrays, names=['first', 'second'])
df = pd.DataFrame([[1, 2, 3, 4], [5, 6, 7, 8]], columns=[1])
print(df)
Drag options to blanks, or click blank then click option'
Apd.MultiIndex(arrays)
Barrays
Cpd.Index(arrays)
Dindex
Attempts:
3 left
💡 Hint
Common Mistakes
Using the original arrays list instead of the MultiIndex object.
Trying to create a new MultiIndex inside the DataFrame constructor.
2fill in blank
medium

Complete the code to set the columns of the DataFrame to a MultiIndex created from tuples.

Pandas
import pandas as pd

cols = [('A', 'one'), ('A', 'two'), ('B', 'one'), ('B', 'two')]
mi = pd.MultiIndex.from_tuples(cols, names=['first', 'second'])
df = pd.DataFrame([[1, 2, 3, 4], [5, 6, 7, 8]])
df.columns = [1]
print(df)
Drag options to blanks, or click blank then click option'
Ami
Bpd.MultiIndex(cols)
Cpd.Index(cols)
Dcols
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning the list of tuples directly to df.columns.
Using pd.Index instead of pd.MultiIndex.
3fill in blank
hard

Fix the error in the code to correctly set a MultiIndex as DataFrame columns.

Pandas
import pandas as pd

arrays = [['X', 'X', 'Y', 'Y'], ['a', 'b', 'a', 'b']]
mi = pd.MultiIndex.from_arrays(arrays)
df = pd.DataFrame([[10, 20, 30, 40], [50, 60, 70, 80]])
df.columns = [1]
print(df)
Drag options to blanks, or click blank then click option'
Apd.Index(arrays)
Barrays
Cmi
Darrays.T
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning the original arrays list instead of the MultiIndex.
Trying to transpose arrays which is not valid here.
4fill in blank
hard

Fill both blanks to create a MultiIndex from product of two lists and set it as DataFrame columns.

Pandas
import pandas as pd

levels = [['A', 'B'], ['one', 'two']]
mi = pd.MultiIndex.[1](levels)
df = pd.DataFrame([[1, 2, 3, 4], [5, 6, 7, 8]])
df.columns = mi.[2](['first', 'second'])
print(df)
Drag options to blanks, or click blank then click option'
Afrom_product
Bfrom_tuples
Cset_names
Drename
Attempts:
3 left
💡 Hint
Common Mistakes
Using from_tuples instead of from_product.
Using rename instead of set_names.
5fill in blank
hard

Fill all three blanks to create a MultiIndex from zipped lists, set names, and assign as DataFrame columns.

Pandas
import pandas as pd

arrays = list(zip(['X', 'X', 'Y', 'Y'], ['a', 'b', 'a', 'b']))
mi = pd.MultiIndex.from_tuples([1])
mi = mi.[2](['level1', 'level2'])
df = pd.DataFrame([[10, 20, 30, 40], [50, 60, 70, 80]])
df.columns = [3]
print(df)
Drag options to blanks, or click blank then click option'
Aarrays
Bset_names
Cmi
Drename
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning the zipped list directly to columns without creating MultiIndex.
Using rename instead of set_names.
Assigning the wrong variable to df.columns.

Practice

(1/5)
1. What is the main purpose of setting columns as a MultiIndex in a pandas DataFrame?
easy
A. To sort columns alphabetically
B. To convert columns into rows
C. To remove duplicate columns
D. To organize columns with multiple header levels for better grouping

Solution

  1. Step 1: Understand MultiIndex columns

    MultiIndex columns allow multiple levels of headers, grouping related columns together.
  2. Step 2: Identify the purpose

    This organization helps in better data analysis and clarity by grouping related columns under common headers.
  3. Final Answer:

    To organize columns with multiple header levels for better grouping -> Option D
  4. Quick Check:

    MultiIndex = multiple header levels [OK]
Hint: MultiIndex groups columns under multiple headers [OK]
Common Mistakes:
  • Confusing MultiIndex with row indexing
  • Thinking MultiIndex removes duplicates
  • Assuming MultiIndex sorts columns automatically
2. Which of the following is the correct syntax to set MultiIndex columns from a list of tuples cols in pandas?
easy
A. df.columns = pd.MultiIndex.from_tuples(cols)
B. df.columns = pd.MultiIndex(cols)
C. df.columns = MultiIndex.from_tuples(cols)
D. df.columns = pd.MultiIndex.tuples(cols)

Solution

  1. Step 1: Recall the correct method

    The pandas method to create MultiIndex from tuples is pd.MultiIndex.from_tuples().
  2. Step 2: Match syntax

    Assigning df.columns = pd.MultiIndex.from_tuples(cols) correctly sets MultiIndex columns.
  3. Final Answer:

    df.columns = pd.MultiIndex.from_tuples(cols) -> Option A
  4. Quick Check:

    Use from_tuples() to create MultiIndex [OK]
Hint: Use pd.MultiIndex.from_tuples() to set MultiIndex columns [OK]
Common Mistakes:
  • Using pd.MultiIndex() directly without from_tuples
  • Missing pd prefix for MultiIndex
  • Using a non-existent method like tuples()
3. Given the code:
import pandas as pd
cols = [('A', 'one'), ('A', 'two'), ('B', 'one'), ('B', 'two')]
data = [[1, 2, 3, 4], [5, 6, 7, 8]]
df = pd.DataFrame(data)
df.columns = pd.MultiIndex.from_tuples(cols)
print(df)

What will be the output?
medium
A. A DataFrame with MultiIndex columns showing levels 'A' and 'B' with sub-levels 'one' and 'two'
B. A DataFrame with single-level columns named 0,1,2,3
C. A DataFrame with columns named ('A', 'one'), ('A', 'two'), ('B', 'one'), ('B', 'two') as strings
D. SyntaxError due to incorrect MultiIndex assignment

Solution

  1. Step 1: Create MultiIndex columns

    The list cols contains tuples representing two levels: first level 'A' or 'B', second level 'one' or 'two'.
  2. Step 2: Assign MultiIndex to df.columns

    Using pd.MultiIndex.from_tuples(cols) sets these tuples as hierarchical column headers.
  3. Final Answer:

    A DataFrame with MultiIndex columns showing levels 'A' and 'B' with sub-levels 'one' and 'two' -> Option A
  4. Quick Check:

    MultiIndex columns display hierarchical headers [OK]
Hint: MultiIndex columns show grouped headers from tuples [OK]
Common Mistakes:
  • Expecting flat column names instead of MultiIndex
  • Confusing tuple strings with MultiIndex tuples
  • Assuming syntax error without checking code
4. What is wrong with this code snippet for setting MultiIndex columns?
import pandas as pd
data = [[1, 2], [3, 4]]
df = pd.DataFrame(data)
cols = [('X', 'a'), ('Y', 'b')]
df.columns = pd.MultiIndex(cols)
medium
A. The list cols should be a list of strings, not tuples
B. DataFrame data shape does not match columns length
C. pd.MultiIndex() cannot take a list of tuples directly; use from_tuples()
D. MultiIndex columns must be set during DataFrame creation

Solution

  1. Step 1: Check MultiIndex creation method

    The code uses pd.MultiIndex(cols), but this constructor does not accept a list of tuples directly.
  2. Step 2: Correct method usage

    The correct way is to use pd.MultiIndex.from_tuples(cols) to create MultiIndex from tuples.
  3. Final Answer:

    pd.MultiIndex() cannot take a list of tuples directly; use from_tuples() -> Option C
  4. Quick Check:

    Use from_tuples() to create MultiIndex from tuples [OK]
Hint: Use from_tuples() to create MultiIndex from tuple list [OK]
Common Mistakes:
  • Using pd.MultiIndex() directly with tuples
  • Ignoring error messages about constructor arguments
  • Trying to set MultiIndex columns without matching data shape
5. You have sales data for two regions and two products:
data = [[100, 200, 150, 250], [110, 210, 160, 260]]
cols = [('Region1', 'ProductA'), ('Region1', 'ProductB'), ('Region2', 'ProductA'), ('Region2', 'ProductB')]
df = pd.DataFrame(data)
How do you set the columns as a MultiIndex to group products under regions, and then select all sales for Region1?
hard
A. Set columns with pd.MultiIndex.from_product(cols) and select with df.loc[:, 'Region1']
B. Set columns with pd.MultiIndex.from_tuples(cols) and select with df.loc[:, 'Region1']
C. Set columns with pd.MultiIndex.from_tuples(cols) and select with df['Region1']
D. Set columns with pd.MultiIndex.from_tuples(cols) and select with df['ProductA']

Solution

  1. Step 1: Set MultiIndex columns correctly

    Use pd.MultiIndex.from_tuples(cols) to create MultiIndex with regions as first level and products as second.
  2. Step 2: Select all sales for Region1

    Use df.loc[:, 'Region1'] to select all columns under the 'Region1' level.
  3. Final Answer:

    Set columns with pd.MultiIndex.from_tuples(cols) and select with df.loc[:, 'Region1'] -> Option B
  4. Quick Check:

    Use from_tuples() and df.loc[:, region] to select [OK]
Hint: Use from_tuples() and df.loc[:, region] to select grouped columns [OK]
Common Mistakes:
  • Using from_product() instead of from_tuples()
  • Selecting columns with df['Region1'] which may fail for MultiIndex
  • Selecting wrong level like 'ProductA' instead of region
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