Given the DataFrame with a MultiIndex, what will be the output of selecting data.loc['B']?
import pandas as pd index = pd.MultiIndex.from_tuples([('A', 1), ('A', 2), ('B', 1), ('B', 2)]) data = pd.DataFrame({'Value': [10, 20, 30, 40]}, index=index) result = data.loc['B'] print(result)
Remember that selecting with a level value returns a DataFrame or Series indexed by the remaining level(s).
When selecting data.loc['B'], pandas returns all rows where the first level of the MultiIndex is 'B'. The resulting DataFrame is indexed by the second level (1 and 2) with the 'Value' column.
Given the MultiIndex DataFrame below, how many rows remain after slicing with data.loc[('A', slice(1,2))]?
import pandas as pd index = pd.MultiIndex.from_tuples([('A', 1), ('A', 2), ('A', 3), ('B', 1)]) data = pd.DataFrame({'Value': [10, 20, 30, 40]}, index=index) sliced = data.loc[('A', slice(1,2))] print(len(sliced))
Slice selects rows where the second level is between 1 and 2 inclusive.
The slice selects rows with first level 'A' and second level 1 or 2. There are exactly two such rows.
Given a MultiIndex DataFrame with sales data, which plot correctly shows the total sales summed by the first level of the index?
import pandas as pd import matplotlib.pyplot as plt index = pd.MultiIndex.from_tuples([('Store1', 'Jan'), ('Store1', 'Feb'), ('Store2', 'Jan'), ('Store2', 'Feb')]) data = pd.DataFrame({'Sales': [100, 150, 200, 250]}, index=index) agg = data.groupby(level=0).sum() agg.plot(kind='bar') plt.show()
Grouping by level=0 sums sales per store.
The groupby(level=0).sum() aggregates sales by the first index level (store). The bar plot shows total sales per store.
What error will this code raise when trying to slice a MultiIndex DataFrame?
import pandas as pd index = pd.MultiIndex.from_tuples([('X', 1), ('X', 2), ('Y', 1)]) data = pd.DataFrame({'Value': [5, 10, 15]}, index=index) result = data.loc['X', 1:2] print(result)
Check how .loc handles multiple indexers with MultiIndex.
Using data.loc['X', 1:2] tries to index with two separate arguments, but .loc expects a tuple for MultiIndex slicing. This causes an IndexingError.
Given a MultiIndex DataFrame, what is the output after swapping the index levels and sorting by the new index?
import pandas as pd index = pd.MultiIndex.from_tuples([('C', 2), ('A', 1), ('B', 3)]) data = pd.DataFrame({'Score': [30, 10, 20]}, index=index) swapped = data.swaplevel(0, 1).sort_index() print(swapped)
Swapping levels changes the order of index levels, then sorting arranges by the new first level.
swaplevel(0,1) switches the two index levels. Sorting by index then orders by the new first level (previously second level). The output shows index with first level 1,2,3 and second level A,C,B respectively.