您好,登錄后才能下訂單哦!
DataFrame 類型類似于數據庫表結構的數據結構,其含有行索引和列索引,可以將DataFrame 想成是由相同索引的Series組成的Dict類型。在其底層是通過二維以及一維的數據塊實現。
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 其它構造方法:
從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
增加一列,并所有賦值為同一個值:
# 增加一列值 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
總結
以上所述是小編給大家介紹的pandas 數據結構之DataFrame,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。