0
0
Pandasdata~20 mins

Creating MultiIndex DataFrames in Pandas - Practice Exercises

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
Output of MultiIndex creation from tuples
What is the output of this code snippet that creates a MultiIndex DataFrame from tuples?
Pandas
import pandas as pd

index = pd.MultiIndex.from_tuples([('A', 1), ('A', 2), ('B', 1)])
df = pd.DataFrame({'Value': [10, 20, 30]}, index=index)
print(df)
A
       Value
A 1      10
  2      20
B 1      30
B
Value
A 1 10
  2 20
B 1 30
C
   Value
A 1    10
  2    20
B 1    30
D
Value
A 1 10
A 2 20
B 1 30
Attempts:
2 left
💡 Hint
Look at how pandas displays MultiIndex DataFrames with hierarchical row labels.
data_output
intermediate
1:30remaining
Number of rows in MultiIndex DataFrame
Given this MultiIndex DataFrame, how many rows does it contain?
Pandas
import pandas as pd

arrays = [['X', 'X', 'Y', 'Y'], [10, 20, 10, 20]]
index = pd.MultiIndex.from_arrays(arrays, names=('Letter', 'Number'))
df = pd.DataFrame({'Score': [5, 15, 10, 20]}, index=index)
print(len(df))
A2
B0
C4
D1
Attempts:
2 left
💡 Hint
Count the number of unique index pairs created.
🔧 Debug
advanced
2:00remaining
Identify the error in MultiIndex creation
What error does this code raise when trying to create a MultiIndex DataFrame?
Pandas
import pandas as pd

index = pd.MultiIndex.from_tuples([('A', 1), ('B',)])
df = pd.DataFrame({'Value': [10, 20]}, index=index)
ATypeError: Cannot create MultiIndex from tuples
BNo error, DataFrame created successfully
CIndexError: tuple index out of range
DValueError: Length of tuples must be same
Attempts:
2 left
💡 Hint
Check if all tuples have the same length for MultiIndex.
🚀 Application
advanced
2:30remaining
Create MultiIndex DataFrame from product of lists
Which code snippet correctly creates a MultiIndex DataFrame with all combinations of ['Red', 'Blue'] and [1, 2] as index levels?
A
import pandas as pd
index = pd.MultiIndex.from_product([['Red', 'Blue'], [1, 2]], names=['Color', 'Number'])
df = pd.DataFrame({'Value': [10, 20, 30, 40]}, index=index)
print(df)
B
import pandas as pd
index = pd.MultiIndex.from_tuples([('Red', 1), ('Blue', 2)])
df = pd.DataFrame({'Value': [10, 20]}, index=index)
print(df)
C
import pandas as pd
index = pd.MultiIndex.from_arrays([['Red', 'Blue'], [1, 2]], names=['Color', 'Number'])
df = pd.DataFrame({'Value': [10, 20]}, index=index)
print(df)
D
import pandas as pd
index = pd.MultiIndex.from_product([['Red', 'Blue'], [1, 2, 3]], names=['Color', 'Number'])
df = pd.DataFrame({'Value': [10, 20, 30, 40]}, index=index)
print(df)
Attempts:
2 left
💡 Hint
Use from_product to get all combinations of the two lists.
🧠 Conceptual
expert
3:00remaining
Understanding MultiIndex level names and access
Given a MultiIndex DataFrame with levels named 'City' and 'Year', which code correctly selects all rows where 'City' is 'Paris'?
Pandas
import pandas as pd

index = pd.MultiIndex.from_tuples([('Paris', 2020), ('Paris', 2021), ('London', 2020)], names=['City', 'Year'])
df = pd.DataFrame({'Population': [2_100_000, 2_150_000, 8_900_000]}, index=index)
Adf.loc[('Paris',)]
Bdf.xs('Paris', level='City')
Cdf.loc['Paris']
Ddf.loc[df.index.get_level_values('City') == 'Paris']
Attempts:
2 left
💡 Hint
Use the cross-section method to select by level name.