0
0
Pandasdata~10 mins

Setting columns as MultiIndex in Pandas - Interactive Code Practice

Choose your learning style9 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.