您的位置:首页 > 其它

pandas层次化索引

2017-12-29 18:03 573 查看
import pandas as pd
import numpy as np
from numpy import nan as NA
df=pd.DataFrame(np.random.randn(7,3),index=['a','b','c','d','e','f','g'],columns=['q','w','t'])
In [106]:df
Out[120]:
q         w         t
a  0.351629 -1.586787  0.557937
b -0.579714  0.497142 -1.949643
c  0.444369 -0.924846  1.210504
d -0.695731  0.399221 -1.152604
e -0.723842  0.523469 -0.443298
f  0.689071  0.388882 -0.268371
g  0.311272  0.637418 -0.935549
df.loc['a':'e','w']=NA
df.loc['a':'c','t']=NA
In [106]: df
Out[123]:
q         w         t
a  0.351629       NaN       NaN
b -0.579714       NaN       NaN
c  0.444369       NaN       NaN
d -0.695731       NaN -1.152604
e -0.723842       NaN -0.443298
f  0.689071  0.388882 -0.268371
g  0.311272  0.637418 -0.935549
In [106]: df.fillna(method='bfill',limit=3)
Out[124]:
q         w         t
a  0.351629       NaN -1.152604
b -0.579714       NaN -1.152604
c  0.444369  0.388882 -1.152604
d -0.695731  0.388882 -1.152604
e -0.723842  0.388882 -0.443298
f  0.689071  0.388882 -0.268371
g  0.311272  0.637418 -0.935549
frame=pd.DataFrame(np.arange(12).reshape((4,3)),index=[['a','a','b','b'],[1,2,1,2]],columns=[['oo','oo','cc'],['gg','rr','gg']])
In [106]: frame#分层索引
Out[106]:
oo      cc
gg  rr  gg
a 1  0   1   2
2  3   4   5
b 1  6   7   8
2  9  10  11
frame.index.names=['key1','key2']
frame.columns.names=['state','color']
In [108]: frame#名字
Out[108]:
state     oo      cc
color     gg  rr  gg
key1 key2
a    1     0   1   2
2     3   4   5
b    1     6   7   8
2     9  10  11
In [108]: frame['oo']#列索引
Out[109]:
color      gg  rr
key1 key2
a    1      0   1
2      3   4
b    1      6   7
2      9  10
In [108]: frame.swaplevel('key1','key2')#接受两个级别编号或名称,返回一个互换级别的新对象
Out[110]:
state     oo      cc
color     gg  rr  gg
key2 key1
1    a     0   1   2
2    a     3   4   5
1    b     6   7   8
2    b     9  10  11
In [108]: frame.sort_index(1)#根据单个级别中的值对数据进行排序
Out[111]:
state      cc oo
color      gg gg  rr
key1 key2
a    1      2
4000
0   1
2      5  3   4
b    1      8  6   7
2     11  9  10
In [108]: frame.swaplevel(0,1).sort_index(0)
Out[112]:
state     oo      cc
color     gg  rr  gg
key2 key1
1    a     0   1   2
b     6   7   8
2    a     3   4   5
b     9  10  11
In [108]: frame.sum(level='key2')#指定在某条轴上求和的级别
Out[114]:
state  oo      cc
color  gg  rr  gg
key2
1       6   8  10
2      12  14  16
In [108]: frame.sum(level='color',axis=1)
Out[117]:
color      gg  rr
key1 key2
a    1      2   1
2      8   4
b    1     14   7
2     20  10
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  pd