日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

DataFrame

發(fā)布時間:2025/7/14 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 DataFrame 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

DataFrame是一個表格型的數(shù)據(jù)結(jié)構(gòu),含有一組有序的列,每列可以是不同的值類型(數(shù)值、字符串、布爾值等),DataFrame就行索引也有列索引,可以被看做由Series組成的字典(共用同一個索引)。跟其他類似的數(shù)據(jù)結(jié)構(gòu)相比,DataFrame中面向行和面向列的操作基本上是平衡的。

構(gòu)建DataFrame的方法有很多,最常用的一種是直接傳入一個由等長列表或Numpy數(shù)組組成的字典。

結(jié)果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一樣,如果傳入的列在數(shù)據(jù)中找不到,就會產(chǎn)生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')

通過類似字典標(biāo)記的方式或?qū)傩缘姆绞?#xff0c;可以將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屬性也已經(jīng)被相應(yīng)地設(shè)置好了。行也可以通過位置或名稱的方式進行獲取,用索引字段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’列附上一個標(biāo)量值或一組值。

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

將列表或數(shù)組賦值給某個列時,其長度必須跟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

為不存在的列賦值會創(chuàng)建出一個新列,關(guān)鍵字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')

通過索引方式返回的列知識相應(yīng)數(shù)據(jù)的視圖,并不是副本。因此,對返回的Series所做的任何就地修改全都會反應(yīng)到源DataFrame上。通過Series的copy()即可顯示地復(fù)制列

轉(zhuǎn)載于:https://www.cnblogs.com/yu-1104/p/7891446.html

總結(jié)

以上是生活随笔為你收集整理的DataFrame的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。