DataFrame 型別類似於資料庫表結構的資料結構,其含有行索引和列索引,可以將DataFrame 想成是由相同索引的Series組成的Dict型別。在其底層是通過二維以及一維的資料塊實現。
1. DataFrame 物件的構建
1.1 用包含等長的列表或者是NumPy陣列的字典建立DataFrame物件
In [68]: import pandas as pd In [69]: from pandas import Series,DataFrame
# 建立包含等長列表的字典型別 In [70]: data = {'state': ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada'],'year': [2000, 2001, 20 ...: 02, 2001, 2002],'pop': [1.5, 1.7, 3.6, 2.4, 2.9]} In [71]: data Out[71]: {'pop': [1.5, 1.7, 3.6, 2.4, 2.9], 'state': ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada'], 'year': [2000, 2001, 2002, 2001, 2002]} # 建立DataFrame物件 In [72]: frame1 = DataFrame(data) # 紅色部分為自動生成的索引 In [73]: frame1 Out[73]: pop state year 0 1.5 Ohio 2000 1 1.7 Ohio 2001 2 3.6 Ohio 2002 3 2.4 Nevada 2001 4 2.9 Nevada 2002
在建立過程中可以指點列的順序:
In [74]: frame1 = DataFrame(data,columns=['year', 'state', 'pop']) In [75]: frame1 Out[75]: year state pop 0 2000 Ohio 1.5 1 2001 Ohio 1.7 2 2002 Ohio 3.6 3 2001 Nevada 2.4 4 2002 Nevada 2.9
和Series一樣,DataFrame也是可以指定索引內容:
In [76]: ind = ['one', 'two', 'three', 'four', 'five'] In [77]: frame1 = DataFrame(data,index = ind) In [78]: frame1 Out[78]: pop state year one 1.5 Ohio 2000 two 1.7 Ohio 2001 three 3.6 Ohio 2002 four 2.4 Nevada 2001 five 2.9 Nevada 2002
1.2. 用由字典型別組成的巢狀字典型別來生成DataFrame物件
當由巢狀的字典型別生成DataFrame的時候,外部的字典索引會成為列名,內部的字典索引會成為行名。生成的DataFrame會根據行索引排序
In [84]: pop = {'Nevada': {2001: 2.4, 2002: 2.9},'Ohio': {2000: 1.5, 2001: 1.7, 2002: 3.6}} In [85]: frame3 = DataFrame(pop) In [86]: frame3 Out[86]: Nevada Ohio 2000 NaN 1.5 2001 2.4 1.7 2002 2.9 3.6
除了使用預設的按照行索引排序之外,還可以指定行序列:
In [95]: frame3 = DataFrame(pop,[2002,2001,2000]) In [96]: frame3 Out[96]: Nevada Ohio 2002 2.9 3.6 2001 2.4 1.7 2000 NaN 1.5
1.3 其它構造方法:
2. DataFrame 內容訪問
從DataFrame中獲取一列的結果為一個Series,可以通過以下兩種方式獲取:
# 以字典索引方式獲取
In [100]: frame1["state"] Out[100]: one Ohio two Ohio three Ohio four Nevada five Nevada Name: state, dtype: object # 以屬性方式獲取 In [101]: frame1.state Out[101]: one Ohio two Ohio three Ohio four Nevada five Nevada Name: state, dtype: object
也可以通過ix獲取一行資料:
In [109]: frame1.ix["one"] # 或者是 frame1.ix[0] Out[109]: pop 1.5 state Ohio year 2000 Name: one, dtype: object
# 獲取多行資料
In [110]: frame1.ix[["tow","three","four"]]
Out[110]:
pop state year
tow NaN NaN NaN
three 3.6 Ohio 2002.0
four 2.4 Nevada 2001.0
# 還可以通過預設數字行索引來獲取資料
In [111]: frame1.ix[range(3)]
Out[111]:
pop state year
one 1.5 Ohio 2000
two 1.7 Ohio 2001
three 3.6 Ohio 2002
獲取指定行,指定列的交匯值:
In [119]: frame1["state"] Out[119]: one Ohio two Ohio three Ohio four Nevada five Nevada Name: state, dtype: object In [120]: frame1["state"][0] Out[120]: 'Ohio' In [121]: frame1["state"]["one"] Out[121]: 'Ohio'
先指定列再指定行:
In [125]: frame1.ix[0] Out[125]: pop 1.5 state Ohio year 2000 Name: one, dtype: object In [126]: frame1.ix[0]["state"] Out[126]: 'Ohio' In [127]: frame1.ix["one"]["state"] Out[127]: 'Ohio' In [128]: frame1.ix["one"][0] Out[128]: 1.5 In [129]: frame1.ix[0][0] Out[129]: 1.5
3. DataFrame 物件的修改
增加一列,並所有賦值為同一個值:
# 增加一列值
In [131]: frame1["debt"] = 10 In [132]: frame1 Out[132]: pop state year debt one 1.5 Ohio 2000 10 two 1.7 Ohio 2001 10 three 3.6 Ohio 2002 10 four 2.4 Nevada 2001 10 five 2.9 Nevada 2002 10
# 更改一列的值 In [133]: frame1["debt"] = np.arange(5) In [134]: frame1 Out[134]: pop state year debt one 1.5 Ohio 2000 0 two 1.7 Ohio 2001 1 three 3.6 Ohio 2002 2 four 2.4 Nevada 2001 3 five 2.9 Nevada 2002 4
追加型別為Series的一列
# 判斷是否為東部區
In [137]: east = (frame1.state == "Ohio") In [138]: east Out[138]: one True two True three True four False five False Name: state, dtype: bool # 賦Series值 In [139]: frame1["east"] = east In [140]: frame1 Out[140]: pop state year debt east one 1.5 Ohio 2000 0 True two 1.7 Ohio 2001 1 True three 3.6 Ohio 2002 2 True four 2.4 Nevada 2001 3 False five 2.9 Nevada 2002 4 False
DataFrame 的行可以命名,同時多列也可以命名:
In [145]: frame3.columns.name = "state" In [146]: frame3.index.name = "year" In [147]: frame3 Out[147]: state Nevada Ohio year 2002 2.9 3.6 2001 2.4 1.7 2000 NaN 1.5