DataFrame
DataFrame是一個表格型的數據結構,含有一組有序的列,每列可以是不同的值類型(數值、字符串、布爾值等),DataFrame就行索引也有列索引,可以被看做由Series組成的字典(共用同一個索引)。跟其他類似的數據結構相比,DataFrame中面向行和面向列的操作基本上是平衡的。
構建DataFrame的方法有很多,最常用的一種是直接傳入一個由等長列表或Numpy數組組成的字典。
結果DataFrame會自動加上索引,且全部列會被有序排列。
1 In [10]: import pandas as ps 2 3 In [11]: from pandas import DataFrame 4 5 In [12]: data = {'state':['simple','simple','Python','Python'],'year':['2017','2 6 ...: 018','2019','2020'],'pop':['1','2','3','4']} 7 8 In [13]: frame = DataFrame(data) 9 10 In [14]: frame 11 Out[14]: 12 pop state year 13 0 1 simple 2017 14 1 2 simple 2018 15 2 3 Python 2019 16 3 4 Python 2020如果指定列順序,DataFrame的列就會按照指定順序進行排列。
1 In [15]: DataFrame(data,columns=['year','state','pop']) 2 Out[15]: 3 year state pop 4 0 2017 simple 1 5 1 2018 simple 2 6 2 2019 Python 3 7 3 2020 Python 4與Series一樣,如果傳入的列在數據中找不到,就會產生NA值。
1 In [17]: frame2 = DataFrame(data,columns=['year','state','pop','debt'],index=['o 2 ...: ne','two','three','four']) 3 4 In [18]: frame2 5 Out[18]: 6 year state pop debt 7 one 2017 simple 1 NaN 8 two 2018 simple 2 NaN 9 three 2019 Python 3 NaN 10 four 2020 Python 4 NaN 11 12 In [19]: frame2.columns 13 Out[19]: Index([u'year', u'state', u'pop', u'debt'], dtype='object')通過類似字典標記的方式或屬性的方式,可以將DataFrame的列獲取為一個Series。
1 In [20]: frame2['state'] 2 Out[20]: 3 one simple 4 two simple 5 three Python 6 four Python 7 Name: state, dtype: object 8 9 In [21]: frame2.year 10 Out[21]: 11 one 2017 12 two 2018 13 three 2019 14 four 2020 15 Name: year, dtype: object返回的Series擁有原DataFrame相同的索引,且其name屬性也已經被相應地設置好了。行也可以通過位置或名稱的方式進行獲取,用索引字段ix
1 In [23]: frame2.ix['three'] 2 Out[23]: 3 year 2019 4 state Python 5 pop 3 6 debt NaN 7 Name: three, dtype: object列可以通過賦值的方式進行修改,給空的‘debt’列附上一個標量值或一組值。
1 In [24]: frame2['debt'] = '10' 2 3 In [25]: frame2 4 Out[25]: 5 year state pop debt 6 one 2017 simple 1 10 7 two 2018 simple 2 10 8 three 2019 Python 3 10 9 four 2020 Python 4 10通過np.arange()為‘debt’列賦值
1 In [29]: frame2['debt'] = np.arange(4.) 2 3 In [30]: frame2 4 Out[30]: 5 year state pop debt 6 one 2017 simple 1 0.0 7 two 2018 simple 2 1.0 8 three 2019 Python 3 2.0 9 four 2020 Python 4 3.0將列表或數組賦值給某個列時,其長度必須跟DataFrame的長度相匹配,如果賦值的是一個Series,就會精確匹配DataFrame的索引,所有的空位都會被填上缺失值。
1 1 In [32]: val = Series([1,3,4],index=['one','three','four']) 2 2 3 3 In [33]: frame2['debt'] = val 4 4 5 5 In [34]: frame2 6 6 Out[34]: 7 7 year state pop debt 8 8 one 2017 simple 1 1.0 9 9 two 2018 simple 2 NaN 10 10 three 2019 Python 3 3.0 11 11 four 2020 Python 4 4.0為不存在的列賦值會創建出一個新列,關鍵字del用于刪除列。。
1 In [35]: frame2['eastern'] = frame2.state=='simple' 2 3 In [36]: frame2 4 Out[36]: 5 year state pop debt eastern 6 one 2017 simple 1 1.0 True 7 two 2018 simple 2 NaN True 8 three 2019 Python 3 3.0 False 9 four 2020 Python 4 4.0 False 10 11 In [37]: del frame2['eastern'] 12 13 In [38]: frame2.columns 14 Out[38]: Index([u'year', u'state', u'pop', u'debt'], dtype='object')通過索引方式返回的列知識相應數據的視圖,并不是副本。因此,對返回的Series所做的任何就地修改全都會反應到源DataFrame上。通過Series的copy()即可顯示地復制列
轉載于:https://www.cnblogs.com/yu-1104/p/7891446.html
總結
- 上一篇: (网页)SQLserver中在上线的项目
- 下一篇: bzoj 2002