MultiIndex (also called Hierarchical Indexing) is an advanced Pandas feature that allows a DataFrame or Series to have multiple levels of indexes. It helps organize complex datasets efficiently and makes it easier to analyze grouped data. MultiIndex is widely used in financial reporting, sales dashboards, inventory management, business intelligence, and data analysis projects. Pandas MultiIndex and Multi-Level Indexing Practice Questions with Solutions help to understand the concepts.
In this chapter, you’ll solve practical questions on creating, accessing, sorting, resetting, and manipulating MultiIndex DataFrames.
1. Python Program to Create a MultiIndex DataFrame
Problem Statement
Write a Python program to create a DataFrame with a MultiIndex using Department and Employee.
Python Solution
import pandas as pd
data = {
"Department": [
"IT",
"IT",
"HR",
"HR"
],
"Employee": [
"Rahul",
"Aman",
"Priya",
"Sneha"
],
"Salary": [
50000,
60000,
45000,
55000
]
}
df = pd.DataFrame(data)
df = df.set_index(
["Department", "Employee"]
)
print(df)
Sample Output
Salary
Department Employee
IT Rahul 50000
Aman 60000
HR Priya 45000
Sneha 55000
Explanation
The set_index() function creates a MultiIndex using two columns.
Concepts Covered
set_index()- MultiIndex
- Hierarchical Index
2. Python Program to Display Index Levels
Problem Statement
Write a Python program to display all levels of a MultiIndex.
Python Solution
import pandas as pd
data = {
"Department": ["IT", "IT", "HR"],
"Employee": ["Rahul", "Aman", "Priya"],
"Salary": [50000, 60000, 45000]
}
df = pd.DataFrame(data)
df = df.set_index(
["Department", "Employee"]
)
print(df.index.names)
Sample Output
['Department', 'Employee']
Explanation
The index.names attribute displays the names of all index levels.
Concepts Covered
index.names- Multi-Level Index
- Index Information
3. Python Program to Access Data from the First Index Level
Problem Statement
Write a Python program to display all employees from the IT department.
Python Solution
import pandas as pd
data = {
"Department": ["IT", "IT", "HR"],
"Employee": ["Rahul", "Aman", "Priya"],
"Salary": [50000, 60000, 45000]
}
df = pd.DataFrame(data)
df = df.set_index(
["Department", "Employee"]
)
print(df.loc["IT"])
Sample Output
Salary
Employee
Rahul 50000
Aman 60000
Explanation
Using .loc["IT"] retrieves all rows belonging to the IT department.
Concepts Covered
.loc- MultiIndex Selection
- First-Level Index
4. Python Program to Access a Specific MultiIndex Record
Problem Statement
Write a Python program to display Rahul’s salary from the IT department.
Python Solution
import pandas as pd
data = {
"Department": ["IT", "IT", "HR"],
"Employee": ["Rahul", "Aman", "Priya"],
"Salary": [50000, 60000, 45000]
}
df = pd.DataFrame(data)
df = df.set_index(
["Department", "Employee"]
)
print(df.loc[("IT", "Rahul")])
Sample Output
Salary 50000
Name: (IT, Rahul), dtype: int64
Explanation
A tuple inside .loc[] is used to access a specific row in a MultiIndex DataFrame.
Concepts Covered
- Tuple Indexing
.loc- Multi-Level Access
5. Python Program to Reset a MultiIndex
Problem Statement
Write a Python program to convert a MultiIndex back into normal columns.
Python Solution
import pandas as pd
data = {
"Department": ["IT", "IT", "HR"],
"Employee": ["Rahul", "Aman", "Priya"],
"Salary": [50000, 60000, 45000]
}
df = pd.DataFrame(data)
df = df.set_index(
["Department", "Employee"]
)
result = df.reset_index()
print(result)
Sample Output
Department Employee Salary
0 IT Rahul 50000
1 IT Aman 60000
2 HR Priya 45000
Explanation
The reset_index() function converts MultiIndex levels back into regular DataFrame columns.
Concepts Covered
reset_index()- MultiIndex
- Index Conversion
6. Python Program to Sort a MultiIndex DataFrame
Problem Statement
Write a Python program to sort a MultiIndex DataFrame by its index.
Python Solution
import pandas as pd
data = {
"Department": [
"HR",
"IT",
"Finance",
"IT"
],
"Employee": [
"Priya",
"Rahul",
"Aman",
"Sneha"
],
"Salary": [
45000,
50000,
70000,
60000
]
}
df = pd.DataFrame(data)
df = df.set_index(
["Department", "Employee"]
)
result = df.sort_index()
print(result)
Sample Output
Salary
Department Employee
Finance Aman 70000
HR Priya 45000
IT Rahul 50000
Sneha 60000
Explanation
The sort_index() function sorts the MultiIndex alphabetically based on each index level.
Concepts Covered
sort_index()- MultiIndex Sorting
- Hierarchical Index
7. Python Program to Display a Particular Index Level
Problem Statement
Write a Python program to display only the Department level from a MultiIndex.
Python Solution
import pandas as pd
data = {
"Department": [
"IT",
"IT",
"HR"
],
"Employee": [
"Rahul",
"Aman",
"Priya"
],
"Salary": [
50000,
60000,
45000
]
}
df = pd.DataFrame(data)
df = df.set_index(
["Department", "Employee"]
)
print(
df.index.get_level_values("Department")
)
Sample Output
Index(['IT', 'IT', 'HR'], dtype='object', name='Department')
Explanation
The get_level_values() method returns all values from a specific index level.
Concepts Covered
get_level_values()- Index Levels
- MultiIndex
8. Python Program to Swap MultiIndex Levels
Problem Statement
Write a Python program to swap the Department and Employee index levels.
Python Solution
import pandas as pd
data = {
"Department": [
"IT",
"IT",
"HR"
],
"Employee": [
"Rahul",
"Aman",
"Priya"
],
"Salary": [
50000,
60000,
45000
]
}
df = pd.DataFrame(data)
df = df.set_index(
["Department", "Employee"]
)
result = df.swaplevel()
print(result)
Sample Output
Salary
Employee Department
Rahul IT 50000
Aman IT 60000
Priya HR 45000
Explanation
The swaplevel() function exchanges the positions of the MultiIndex levels.
Concepts Covered
swaplevel()- Hierarchical Index
- MultiIndex
9. Python Program to Rename MultiIndex Levels
Problem Statement
Write a Python program to rename MultiIndex level names.
Python Solution
import pandas as pd
data = {
"Department": [
"IT",
"HR"
],
"Employee": [
"Rahul",
"Priya"
],
"Salary": [
50000,
45000
]
}
df = pd.DataFrame(data)
df = df.set_index(
["Department", "Employee"]
)
df.index = df.index.set_names(
["Dept", "Emp"]
)
print(df)
Sample Output
Salary
Dept Emp
IT Rahul 50000
HR Priya 45000
Explanation
The set_names() method changes the names of the MultiIndex levels.
Concepts Covered
set_names()- Rename Index
- Multi-Level Index
10. Python Program to Select Multiple Departments from a MultiIndex
Problem Statement
Write a Python program to display records belonging to the IT and HR departments.
Python Solution
import pandas as pd
data = {
"Department": [
"IT",
"HR",
"Finance",
"IT"
],
"Employee": [
"Rahul",
"Priya",
"Aman",
"Sneha"
],
"Salary": [
50000,
45000,
70000,
60000
]
}
df = pd.DataFrame(data)
df = df.set_index(
["Department", "Employee"]
)
result = df.loc[
["IT", "HR"]
]
print(result)
Sample Output
Salary
Department Employee
IT Rahul 50000
Sneha 60000
HR Priya 45000
Explanation
Passing a list inside .loc[] retrieves records from multiple first-level indexes.
Concepts Covered
.loc- Multiple Index Selection
- MultiIndex Filtering
11. Python Program to Remove One Level of a MultiIndex
Problem Statement
Write a Python program to remove the Department level from a MultiIndex.
Python Solution
import pandas as pd
data = {
"Department": [
"IT",
"IT",
"HR"
],
"Employee": [
"Rahul",
"Aman",
"Priya"
],
"Salary": [
50000,
60000,
45000
]
}
df = pd.DataFrame(data)
df = df.set_index(
["Department", "Employee"]
)
result = df.droplevel("Department")
print(result)
Sample Output
Salary
Employee
Rahul 50000
Aman 60000
Priya 45000
Explanation
The droplevel() function removes a specified index level while keeping the remaining levels.
Concepts Covered
droplevel()- MultiIndex
- Index Manipulation
12. Python Program to Create a MultiIndex from Arrays
Problem Statement
Write a Python program to create a MultiIndex using arrays.
Python Solution
import pandas as pd
departments = [
"IT",
"IT",
"HR",
"HR"
]
employees = [
"Rahul",
"Aman",
"Priya",
"Sneha"
]
index = pd.MultiIndex.from_arrays(
[departments, employees],
names=[
"Department",
"Employee"
]
)
df = pd.DataFrame(
{
"Salary": [
50000,
60000,
45000,
55000
]
},
index=index
)
print(df)
Sample Output
Salary
Department Employee
IT Rahul 50000
Aman 60000
HR Priya 45000
Sneha 55000
Explanation
The MultiIndex.from_arrays() method creates a hierarchical index directly from multiple arrays.
Concepts Covered
MultiIndex.from_arrays()- Hierarchical Index
- Custom Index
13. Python Program to Create a MultiIndex from Tuples
Problem Statement
Write a Python program to create a MultiIndex using tuples.
Python Solution
import pandas as pd
index = pd.MultiIndex.from_tuples(
[
("IT", "Rahul"),
("IT", "Aman"),
("HR", "Priya")
],
names=[
"Department",
"Employee"
]
)
df = pd.DataFrame(
{
"Salary": [
50000,
60000,
45000
]
},
index=index
)
print(df)
Sample Output
Salary
Department Employee
IT Rahul 50000
Aman 60000
HR Priya 45000
Explanation
The MultiIndex.from_tuples() function creates a hierarchical index from tuple values.
Concepts Covered
MultiIndex.from_tuples()- Tuple Index
- Multi-Level Index
14. Python Program to Sort by a Specific MultiIndex Level
Problem Statement
Write a Python program to sort a MultiIndex DataFrame by the Employee level.
Python Solution
import pandas as pd
data = {
"Department": [
"IT",
"HR",
"Finance",
"IT"
],
"Employee": [
"Rahul",
"Sneha",
"Aman",
"Priya"
],
"Salary": [
50000,
55000,
70000,
60000
]
}
df = pd.DataFrame(data)
df = df.set_index(
["Department", "Employee"]
)
result = df.sort_index(
level="Employee"
)
print(result)
Sample Output
Salary
Department Employee
Finance Aman 70000
IT Priya 60000
Rahul 50000
HR Sneha 55000
Explanation
The sort_index(level=...) function sorts records based on a particular MultiIndex level.
Concepts Covered
sort_index()- Level-wise Sorting
- MultiIndex
15. Python Program to Convert MultiIndex into Columns
Problem Statement
Write a Python program to convert every MultiIndex level into normal DataFrame columns.
Python Solution
import pandas as pd
data = {
"Department": [
"IT",
"HR",
"Finance"
],
"Employee": [
"Rahul",
"Priya",
"Aman"
],
"Salary": [
50000,
45000,
70000
]
}
df = pd.DataFrame(data)
df = df.set_index(
["Department", "Employee"]
)
result = df.reset_index()
print(result)
Sample Output
Department Employee Salary
0 IT Rahul 50000
1 HR Priya 45000
2 Finance Aman 70000
Explanation
The reset_index() function converts all MultiIndex levels back into regular DataFrame columns.
Concepts Covered
reset_index()- MultiIndex Conversion
- DataFrame Structure
Chapter Summary
In this chapter, you learned how to work with MultiIndex (Hierarchical Indexing) in Pandas. You practiced creating MultiIndexes using columns, arrays, and tuples, accessing records with .loc, displaying index levels, swapping and renaming index levels, sorting MultiIndexes, selecting multiple index values, removing index levels, and converting MultiIndexes back into regular DataFrame columns. These techniques are commonly used in financial reporting, sales analysis, business intelligence dashboards, and complex hierarchical datasets.
Key Takeaways
set_index()creates a MultiIndex from one or more columns.MultiIndex.from_arrays()creates a hierarchical index from arrays.MultiIndex.from_tuples()creates a hierarchical index from tuples..loc[]retrieves records from MultiIndex DataFrames.get_level_values()returns values from a specific index level.swaplevel()exchanges MultiIndex levels.set_names()renames MultiIndex levels.sort_index(level=...)sorts data by a specific level.droplevel()removes unwanted index levels.reset_index()converts MultiIndexes back into regular columns.
Frequently Asked Questions (FAQs)
1. What is a MultiIndex in Pandas?
A MultiIndex (Hierarchical Index) allows a DataFrame or Series to use multiple index levels instead of a single index.
2. How do you create a MultiIndex from columns?
df.set_index(
["Department", "Employee"]
)
3. How do you access records from a MultiIndex DataFrame?
df.loc["IT"]
4. How do you create a MultiIndex from arrays?
pd.MultiIndex.from_arrays(
[array1, array2]
)
5. How do you create a MultiIndex from tuples?
pd.MultiIndex.from_tuples(
tuples
)
6. How do you remove one index level?
df.droplevel("Department")
7. How do you sort a MultiIndex by a specific level?
df.sort_index(
level="Employee"
)
8. Why is MultiIndex useful in Pandas?
MultiIndex helps organize complex datasets with multiple grouping levels, making data analysis, reporting, pivot tables, financial analysis, and business intelligence tasks more efficient and easier to manage.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
