0
0
Pandasdata~15 mins

Swapping index levels in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Swapping Index Levels in a MultiIndex DataFrame
📖 Scenario: You work in a sales department and have a table showing sales data indexed by Region and Year. You want to rearrange the table so that Year comes before Region in the index.
🎯 Goal: Create a pandas DataFrame with a MultiIndex of Region and Year, then swap the index levels so that Year is the first level and Region is the second.
📋 What You'll Learn
Create a pandas DataFrame with MultiIndex using Region and Year
Add a column Sales with given values
Swap the index levels so that Year is the first level
Ensure the DataFrame index reflects the swapped levels
💡 Why This Matters
🌍 Real World
Swapping index levels helps when you want to reorganize data views, such as changing the priority of grouping in sales reports or time series data.
💼 Career
Data analysts and scientists often need to manipulate MultiIndex DataFrames to prepare data for analysis or visualization.
Progress0 / 4 steps
1
Create the initial MultiIndex DataFrame
Import pandas as pd. Create a MultiIndex from the tuples [('North', 2020), ('North', 2021), ('South', 2020), ('South', 2021)] with names 'Region' and 'Year'. Then create a DataFrame called df with this MultiIndex and a Sales column containing [250, 300, 200, 400].
Pandas
Need a hint?

Use pd.MultiIndex.from_tuples() to create the index and pass it to pd.DataFrame() with the sales data.

2
Add a variable for the new index order
Create a list called new_order with the values ['Year', 'Region'] to represent the desired order of index levels.
Pandas
Need a hint?

Just create a list with the new order of index level names.

3
Swap the index levels using the new order
Use the df.reorder_levels() method with the new_order list to reorder the index levels of df. Assign the result back to df.
Pandas
Need a hint?

Use df.reorder_levels(new_order) to swap the index levels.

4
Verify the index level names after swapping
Assign the list of index level names of df to a variable called index_names using df.index.names.
Pandas
Need a hint?

Use df.index.names to get the list of index level names.