Create Multiindex DataFrame in pandas 🐼

Learn how to build a pandas DataFrame with Multiindex using set_index in Python. Example included for clarity.

Create Multiindex DataFrame in pandas 🐼
Statistics Globe
495 views β€’ Jul 11, 2022
Create Multiindex DataFrame in pandas 🐼

About this video

How to construct a pandas DataFrame with multiindex in the Python programming language. More details: https://statisticsglobe.com/create-pandas-dataframe-multiindex-python
Python code of this video:

import pandas as pd # Load pandas library

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

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

Follow me on Social Media:
Facebook – Statistics Globe Page: https://www.facebook.com/statisticsglobecom/
Facebook – R Programming Group for Discussions & Questions: https://www.facebook.com/groups/statisticsglobe
Facebook – Python Programming Group for Discussions & Questions: https://www.facebook.com/groups/statisticsglobepython
LinkedIn – Statistics Globe Page: https://www.linkedin.com/company/statisticsglobe/
LinkedIn – R Programming Group for Discussions & Questions: https://www.linkedin.com/groups/12555223/
LinkedIn – Python Programming Group for Discussions & Questions: https://www.linkedin.com/groups/12673534/
Twitter: https://twitter.com/JoachimSchork

Music by bensound.com

Tags and Topics

Browse our collection to discover more content in these categories.

Video Information

Views

495

Likes

10

Duration

2:52

Published

Jul 11, 2022

Related Trending Topics

LIVE TRENDS

Related trending topics. Click any trend to explore more videos.

Trending Now