There are several ways to add a level to a MultiIndex in Pandas, depending on your desired outcome. Here are a couple of common approaches:
1. Using set_index()
If you have a column that you want to add as a new level to the index, you can use the set_index() method.
import pandas as pd data = {'Region': ['North', 'North', 'South', 'South'], 'Category': ['Electronics', 'Clothing', 'Electronics', 'Clothing'], 'Sales': [10, 15, 20, 25], 'Profit': [2, 3, 4, 5]} df = pd.DataFrame(data) df = df.set_index(['Region', 'Category']) print(df)
This code will create a MultiIndex with ‘Region’ and ‘Category’ as the levels.
2. Using MultiIndex.from_product()
If you want to add a new level with a constant value to all rows, you can use MultiIndex.from_product().
import pandas as pd df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}, index=['X', 'Y', 'Z']) df.index = pd.MultiIndex.from_product([['Level1'], df.index], names=['Level1', 'Level2']) print(df)
This code will add a new level called ‘Level1’ with the value ‘Level1’ for all rows.
3. Using droplevel() (to add a level with a single value)
While droplevel() is used to remove levels, we can cleverly use it in conjunction with other operations to add a level with a single value:
import pandas as pd df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}, index=['X', 'Y', 'Z']) # Create a new index with an additional level new_index = pd.MultiIndex.from_product([['NewLevel'], df.index]) # Assign the new index to the DataFrame df.index = new_index # Set the names for the levels (optional) df.index.names = ['Level1', 'Level2'] print(df)
This method first creates a new MultiIndex using from_product() with the desired new level (‘NewLevel’ in this case). It then assigns this new index to the DataFrame. Finally, it sets the names for the levels using df.index.names.
These are just a few ways to add levels to a MultiIndex in Pandas. The best approach will depend on your specific needs and the structure of your data.