pandas中的Series和DataFrame

1.Series介紹及創(chuàng)建

站在用戶的角度思考問題,與客戶深入溝通,找到殷都網(wǎng)站設(shè)計(jì)與殷都網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名與空間、虛擬主機(jī)、企業(yè)郵箱。業(yè)務(wù)覆蓋殷都地區(qū)。

Series是一種類似與一維數(shù)組的對象,由下面兩個(gè)部分組成:

values:一組數(shù)據(jù)(ndarray類型)

index:相關(guān)的數(shù)據(jù)索引標(biāo)簽

創(chuàng)建Series的兩種方式:

第一種:由列表或numpy數(shù)組創(chuàng)建:

s1 =Series([11,22,33,44,55],index=['a1','b1','c1','d1','e1'],name='Hello world')

print(s1)

運(yùn)行結(jié)果:

a1 11

b1 22

c1 33

d1 44

e1 55

Name: Hello world, dtype: int64

a1 = np.array([11,22,33,44,55])

s2 = Series(a1,index=['a1','b1','c1','d1','e1'],name='hello series')

print(s2)

運(yùn)行結(jié)果:

a1 11

b1 22

c1 33

d1 44

e1 55

Name: hello series, dtype: int32

第二種:由字典創(chuàng)建,不存在index參數(shù)設(shè)置,但是依然存在默認(rèn)索引(數(shù)據(jù)源必須為一維數(shù)據(jù))

dict = {'hello':12,'series':30}

s3 = Series(data=dict)

print(s3)

運(yùn)行結(jié)果:

hello 12

series 30

dtype: int64

2.DataFrame的介紹及創(chuàng)建

DataFrame具有標(biāo)記軸(行和列)的二維大小可變,可能異構(gòu)的表格數(shù)據(jù)結(jié)構(gòu)

算術(shù)運(yùn)算在行標(biāo)簽和列標(biāo)簽上對齊

可以被認(rèn)為是Series對象的類似dict的容器

是pandas的主要數(shù)據(jù)結(jié)構(gòu)

創(chuàng)建DataFrame的4種方式:

1.使用字典創(chuàng)建DataFarme

dicts = {"tag1": [90, 22, 66],'tag2': [12, 33, 66]}

d1 = DataFrame(data=dicts, index=['a', 'b', 'c'])

print(d1)

運(yùn)行結(jié)果:

tag1 tag2

a 90 12

b 22 33

c 66 66

2.使用ndarray創(chuàng)建DataFrame

d2 = DataFrame(data=np.random.randint(0,100,size=(3,6)),index=["one","two","three"],columns=["a","b","c","d","e","f"])

print(d2)

運(yùn)行結(jié)果:無錫人流醫(yī)院 http://xmobile.wxbhnk120.com/

a b c d e f

one 62 74 51 29 98 18

two 16 16 44 3 64 72

three 42 94 46 60 34 59

3.隱式構(gòu)造

最常見的方法是給DataFrame構(gòu)造函數(shù)的index或者columns參數(shù)傳遞兩個(gè)或更多的數(shù)組(如下另個(gè)列的標(biāo)簽數(shù)組)

d3 = DataFrame(data=np.random.randint(0, 100, size=(2, 4)), index=['x', 'y'], columns=[['a', 'b', 'c', 'd'], ['q1', 'q2', 'q3', 'q4']])

print(d3)

運(yùn)行結(jié)果:

a b c d

q1 q2 q3 q4

x 47 26 11 8

y 40 76 18 9

4.顯示構(gòu)造

使用pd.MultiIndex.from_arrays數(shù)組方式

創(chuàng)建了一個(gè)索引對象,該索引對象為二層索引

indexObj = pd.MultiIndex.from_arrays([['q1', 'q2', 'q3', 'q1'], ['a', 'b', 'c', 'd']])

d4 = DataFrame(data=np.random.randint(0, 100, size=(2, 4)), index=['x', 'y'], columns=indexObj)

print(d4)

運(yùn)行結(jié)果:

q1 q2 q3 q1

a b c d

x 85 72 43 4

y 8 43 55 68

新聞標(biāo)題:pandas中的Series和DataFrame
本文地址:http://bm7419.com/article48/gihgep.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設(shè)外貿(mào)建站、云服務(wù)器、定制開發(fā)、網(wǎng)站策劃網(wǎng)站維護(hù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司