0
0
Pandasdata~20 mins

Setting columns as MultiIndex in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MultiIndex Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this code when setting MultiIndex columns?

Consider the following pandas DataFrame and code that sets MultiIndex columns. What will be the output of print(df.columns)?

Pandas
import pandas as pd

cols = [('A', 'foo'), ('A', 'bar'), ('B', 'baz')]
df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=pd.MultiIndex.from_tuples(cols))
print(df.columns)
AIndex(['A', 'foo', 'A', 'bar', 'B', 'baz'], dtype='object')
BMultiIndex([('A', 'foo'), ('A', 'bar'), ('B', 'baz')], )
CMultiIndex([('foo', 'A'), ('bar', 'A'), ('baz', 'B')], )
DIndex(['foo', 'bar', 'baz'], dtype='object')
Attempts:
2 left
💡 Hint

Look at how pd.MultiIndex.from_tuples creates a MultiIndex from a list of tuples.

data_output
intermediate
1:30remaining
How many columns does the DataFrame have after setting MultiIndex columns?

Given the code below, how many columns does the DataFrame df have?

Pandas
import pandas as pd

arrays = [['one', 'one', 'two', 'two'], ['A', 'B', 'A', 'B']]
cols = pd.MultiIndex.from_arrays(arrays, names=['level_1', 'level_2'])
df = pd.DataFrame([[1, 2, 3, 4], [5, 6, 7, 8]], columns=cols)
print(len(df.columns))
A2
B1
C8
D4
Attempts:
2 left
💡 Hint

Count the number of tuples in the MultiIndex columns.

🔧 Debug
advanced
2:00remaining
What error does this code raise when setting MultiIndex columns incorrectly?

What error will this code raise?

import pandas as pd
cols = [('A', 'foo'), ('B',)]
df = pd.DataFrame([[1, 2], [3, 4]], columns=pd.MultiIndex.from_tuples(cols))
AValueError: All tuples must be the same length
BTypeError: Cannot create MultiIndex from tuples
CIndexError: tuple index out of range
DNo error, runs successfully
Attempts:
2 left
💡 Hint

Check if all tuples have the same length when creating MultiIndex.

visualization
advanced
2:30remaining
Which option correctly creates a DataFrame with MultiIndex columns and displays it?

Which code snippet will create a DataFrame with MultiIndex columns and display it correctly?

A
import pandas as pd
cols = pd.MultiIndex.from_arrays([['X', 'X', 'Y'], ['a', 'b']])
df = pd.DataFrame([[1,2,3],[4,5,6]], columns=cols)
print(df)
B
import pandas as pd
cols = pd.Index([('X', 'a'), ('X', 'b'), ('Y', 'a')])
df = pd.DataFrame([[1,2,3],[4,5,6]], columns=cols)
print(df)
C
import pandas as pd
cols = pd.MultiIndex.from_tuples([('X', 'a'), ('X', 'b'), ('Y', 'a')])
df = pd.DataFrame([[1,2,3],[4,5,6]], columns=cols)
print(df)
D
import pandas as pd
cols = [('X', 'a'), ('X', 'b'), ('Y', 'a')]
df = pd.DataFrame([[1,2,3],[4,5,6]], columns=cols)
print(df)
Attempts:
2 left
💡 Hint

Check how MultiIndex is created and that arrays have matching lengths.

🚀 Application
expert
2:00remaining
After setting MultiIndex columns, what is the value at ('B', 'two') for row 1?

Given the DataFrame below, what is the value at the column ('B', 'two') for the second row (index 1)?

Pandas
import pandas as pd

arrays = [['A', 'A', 'B', 'B'], ['one', 'two', 'one', 'two']]
cols = pd.MultiIndex.from_arrays(arrays)
data = [[10, 20, 30, 40], [50, 60, 70, 80]]
df = pd.DataFrame(data, columns=cols)
value = df.loc[1, ('B', 'two')]
print(value)
A80
B70
C60
D40
Attempts:
2 left
💡 Hint

Look at the column labels and the row index carefully.