Explain the concept of hierarchical indexing with the help of suitable example.
Index
Hierarchy
•
Hierarchical indexing is also known as multiple
indexing. The multiple index is an array of tuples where each tuple is
unique.
•
We can create a MultiIndex from array of arrays using from_arrays() method. For example
state = ['Maharashtra','Maharashtra','Maharashtra",
"Tamilnadu',"Tamilnadu', 'Tamilnadu', 'Gujrat',
'Gujrat','Gujrat']
city = ['Mumbai', 'Pune', 'Nasik', 'Chennai', 'Madurai',
'Puducherry', 'Ahmedabad', 'Surat', 'Vadodara']
population
=
[24973000,8231000,1486053,12395000,1561129,244377,8009000,6538000,2065771]
# create array of arrays
index_array = [state, city]
# create multiindex from array
multi_index = pd.MultiIndex.from_arrays(index_array,
names=['State', 'City'])
# create dataframe using multiindex
df = pd.DataFrame({'Population' :population}, index=multi_index)
print(df)

Code explanation:
In above code,
We
have created a dataframe of state, city and population. We have chosen three
states and three cities from each state to represent their population.
We
have created a MultiIndex object named
multi_index from two arrays: state and city.
We
then created a DataFrame using the population
array and assigned multi_index as
its index. For better understanding just observe the above output.
1. Explain the concept
of hierarchical indexing with the help of suitable example.
Python for Data Science: Chapter 5: NumPy and Pandas Libraries : Tag: Computer Programming, Python, Data Science : Python library - Pandas: Index Hierarchy
Python for Data Science
AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation
English Essentials II
EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Tamils and Technology தமிழர்களும் தொழில்நுட்பமும்
UC25H02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Linear Algebra
MA25C02 2nd Semester | 2025 Regulation
Applied Physics (CSIE) II
PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Digital Principles and Computer Organization
CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Basic Electrical and Electronics Engineering
EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Python for Data Science
AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Re-Engineering for Innovation
ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Python for Data Science - Laboratory
AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation