-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmultiindex.py
More file actions
21 lines (16 loc) · 742 Bytes
/
multiindex.py
File metadata and controls
21 lines (16 loc) · 742 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# multiindex.py
import pandas as pd
import numpy as np
multiIndex = pd.MultiIndex.from_arrays([
['Geagle', 'Geagle', 'Geagle', 'Geagle',
'Epple', 'Epple', 'Epple', 'Epple', 'Macrosoft',
'Macrosoft', 'Macrosoft', 'Macrosoft', ],
['S1', 'S2', 'S3', 'S4', 'S1', 'S2', 'S3', 'S4', 'S1', 'S2', 'S3', 'S4']],
names=('Company', 'Turnover'))
print("multiIndex = \n{}\n".format(multiIndex));
df = pd.DataFrame(data=np.random.randint(0, 1000, 36).reshape(-1, 12),
index=[2016, 2017, 2018],
columns=multiIndex)
print("df = \n{}\n".format(df))
print("2017 S1: \n{}\n".format(df.loc[2017, (['Geagle', 'Epple', 'Macrosoft'] ,'S1')]))
print("Geagle 2018: \n{}\n".format(df.loc[2018, 'Geagle']))