Create pandas DataFrame with Multiindex in Python (Example)
In this Python tutorial you’ll learn how to construct a pandas DataFrame with multiindex.
The post consists of this information:
Let’s dig in…
Example Data & Libraries
First, we need to import the pandas library.
import pandas as pd # Load pandas library
As a next step, let’s also construct some example data in Python:
data = pd.DataFrame({'ID1':range(101, 106), # Create pandas DataFrame 'ID2':[1001, 1001, 1001, 1002, 1002], 'x1':range(1, 6), 'x2':range(15, 10, - 1), 'x3':['x', 'y', 'x', 'x', 'y']}) print(data) # Print pandas DataFrame # ID1 ID2 x1 x2 x3 # 0 101 1001 1 15 x # 1 102 1001 2 14 y # 2 103 1001 3 13 x # 3 104 1002 4 12 x # 4 105 1002 5 11 y
The previous Python console output shows the structure of our example data: It’s a pandas DataFrame with several ID columns.
Example: Create pandas DataFrame with Multiindex Using set_index() Function
In this example, I’ll show how to convert multiple variables of a pandas DataFrame to a multiindex.
To do this, we can use the set_index function as shown below:
data_new = data.set_index(['ID1', 'ID2']) # Apply set_index function print(data_new) # Print DataFrame with multiindex # x1 x2 x3 # ID1 ID2 # 101 1001 1 15 x # 102 1001 2 14 y # 103 1001 3 13 x # 104 1002 4 12 x # 105 1002 5 11 y
The previous output shows that we have created a new pandas DataFrame where the first two ID columns have been set as a multiindex.
Video, Further Resources & Summary
In case you need further explanations on the Python programming codes of this tutorial, I recommend watching the following video on my YouTube channel. I explain the Python programming code of this article in the video.
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Furthermore, you could have a look at the related tutorials on this homepage. A selection of interesting tutorials is shown below.
- How to Use the pandas Library in Python
- Replace NaN by Empty String in pandas DataFrame in Python
- Append pandas DataFrame in Python in R
- Append Values to pandas DataFrame in Python
- Reverse pandas DataFrame in Python
- Create Empty pandas DataFrame in Python
- All Python Programming Examples
This article has demonstrated how to create a pandas DataFrame with multiple indices in the Python programming language. Let me know in the comments, in case you have further questions. In addition, don’t forget to subscribe to my email newsletter to get regular updates on new posts.