Leveraging MultiIndex for High-Dimensional Data Representation

Instruction: Explain how to use MultiIndex objects in Pandas for representing high-dimensional data in a tabular format.

Context: Tests the candidate's ability to work with MultiIndex for complex data organization, enhancing data analysis and manipulation capabilities.

Updated

Official answer available

Read the opening below, then unlock the full answer and practical guidance.

A MultiIndex puts several label levels on a row or column axis. The DataFrame remains two-dimensional; the levels encode compound coordinates and need not include every combination.

import pandas as pd df = pd.DataFrame({"platform": ["desktop", "mobile", "mobile"], "country": ["US", "US", "CA"], "city": ["NY", "SF", "Vancouver"], "visits": [10, 20, 30]}).set_index(["platform", "country", "city"]).sort_index() us_mobile = df.loc[("mobile", "US"), :] assert us_mobile.index.tolist() ==...

Related Questions