import pandas as pd
import numpy as np
from pandas import Series,DataFrame
Series的多重索引
letters = list('ABC')
numbers = list(range(5))
mi = pd.MultiIndex.from_product([letters,numbers])
s1 = Series(np.random.rand(15),index = mi)
print(s1)
A 0 0.929111
1 0.922470
2 0.308488
3 0.121715
4 0.653796
B 0 0.750678
1 0.927841
2 0.906116
3 0.009765
4 0.243549
C 0 0.745671
1 0.407037
2 0.303508
3 0.977439
4 0.011076
dtype: float64
多重索引查询
s1.loc[:,[0,3,4]]
Out[35]:
A 0 0.558897
3 0.082423
4 0.627376
B 0 0.123782
3 0.635579
4 0.056387
C 0 0.028698
3 0.189675
4 0.158884
dtype: float64
切片
s1.loc[pd.IndexSlice[:'B',:2]]
Out[42]:
A 0 0.558897
1 0.262638
2 0.023349
B 0 0.123782
1 0.698727
2 0.182843
dtype: float64
DataFrame 多重索引
df1 = DataFrame(np.arange(12).reshape(4,3),
index=[list('AABB'),list('1212')],
columns = list('abc'))
df1
Out[58]:
a b c
A 1 0 1 2
2 3 4 5
B 1 6 7 8
2 9 10 11
给索引命名
df1.index.names = ['F','S']
df1
Out[72]:
a b c
F S
A 1 0 1 2
2 3 4 5
B 1 6 7 8
2 9 10 11
按索引求和
df1.groupby('F').sum()
Out[66]:
a b c
F
A 3 5 7
B 15 17 19
行列名称转换
df1.stack()
Out[68]:
F S
A 1 a 0
b 1
c 2
2 a 3
b 4
c 5
B 1 a 6
b 7
c 8
2 a 9
b 10
c 11
dtype: int32
索引转换
df1.unstack()
Out[70]:
a b c
S 1 2 1 2 1 2
F
A 0 3 1 4 2 5
B 6 9 7 10 8 11