How to construct a pandas DataFrame with multiindex in the Python programming language. More details: [ Ссылка ]
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: [ Ссылка ]
Facebook – R Programming Group for Discussions & Questions: [ Ссылка ]
Facebook – Python Programming Group for Discussions & Questions: [ Ссылка ]
LinkedIn – Statistics Globe Page: [ Ссылка ]
LinkedIn – R Programming Group for Discussions & Questions: [ Ссылка ]
LinkedIn – Python Programming Group for Discussions & Questions: [ Ссылка ]
Twitter: [ Ссылка ]
Music by bensound.com
![](https://s2.save4k.ru/pic/5kGgXmeQpSQ/maxresdefault.jpg)