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 1012
2345
b 1678
291011
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 101
2234
5b 167
82910
11in [108]: frame['oo']#列索引
out[109]:
color gg rr
key1 key2
a 101
234b 167
2910in [108]: frame.swaplevel('key1','key2')#接受兩個級別編號或名稱,返回乙個互換級別的新物件
out[110]:
state oo cc
color gg rr gg
key2 key1
1 a 012
2 a 345
1 b 678
2 b 9
1011
in [108]: frame.sort_index(1)#根據單個級別中的值對資料進行排序
out[111]:
state cc oo
color gg gg rr
key1 key2
a 120
1253
4b 186
72119
10in [108]: frame.swaplevel(0,1).sort_index(0)
out[112]:
state oo cc
color gg rr gg
key2 key1
1 a 012
b 678
2 a 345
b 9
1011
in [108]: frame.sum(level='key2')#指定在某條軸上求和的級別
out[114]:
state oo cc
color gg rr gg
key2 16
810212
1416
in [108]: frame.sum(level='color',axis=1)
out[117]:
color gg rr
key1 key2
a 121
284b 1147
22010
pandas層次化索引
import numpy as np import pandas as pd from pandas import series,dateframe dd dataframe np.random.rand 4,2 columns data1 data2 index a a b b 1 2 1 2 d...
pandas 設定設定多層次索引
多層次索引 series 生成的excel格式 生成的html格式 對應的 import pandas as pd tup 34 21 4000 0003 34 21 4000 0004 36 15 1900 0011 index pd.multiindex.from tuples tup,name...
小呆學資料分析 pandas中的層次化索引
層次化索引應用場景 例子王小呆的女朋友李小妹是高三老師,高考結束可以查分後,李小妹讓班級裡面的同學將查分結果彙總給她,形式都是 張三.csv 李四.csv 王五.csv,內容如下 張三.csv 姓名,科目,分數 張三,英語,128 張三,語文,110 張三,數學,125 張三,理綜,238李四.cs...