溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

pd.Series()函數(shù)怎么用

發(fā)布時(shí)間:2021-12-03 13:53:36 來源:億速云 閱讀:1516 作者:小新 欄目:編程語言

這篇文章將為大家詳細(xì)講解有關(guān)pd.Series()函數(shù)怎么用,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

  1. Series介紹

  Pandas模塊的數(shù)據(jù)結(jié)構(gòu)主要有兩:1、Series ;2、DataFrame

  series是一個(gè)一維數(shù)組,是基于NumPy的ndarray結(jié)構(gòu)。Pandas會(huì)默然用0到n-1來作為series的index,但也可以自己指定index(可以把index理解為dict里面的key)。

  2. Series創(chuàng)建

  pd.Series([list],index=[list])

  參數(shù)為list;index為可選參數(shù),若不填寫則默認(rèn)index從0開始;若填寫則index長度應(yīng)該與value長度相等。

  import pandas as pd

  s=pd.Series([1,2,3,4,5],index=['a','b','c','f','e'])

  print s

  pd.Series({dict})

  以一字典結(jié)構(gòu)為參數(shù)。

  import pandas as pd

  s=pd.Series({'a':1,'b':2,'c':3,'f':4,'e':5})

  print s

  3. Series取值

  s[index] or s[[index的list]]

  取值操作類似數(shù)組,當(dāng)取不連續(xù)的多個(gè)值時(shí)可以以list為參數(shù)

  import pandas as pd

  import numpy as np

  v = np.random.random_sample(50)

  s = pd.Series(v)

  s1 = s[[3, 13, 23, 33]]

  s2 = s[3:13]

  s3 = s[43]

  print("s1", s1)

  print("s2", s2)

  print("s3", s3)

  s1 3 0.064095

  13 0.354023

  23 0.225739

  33 0.959288

  dtype: float64

  s2 3 0.064095

  4 0.405651

  5 0.024181

  6 0.367606

  7 0.844005

  8 0.405313

  9 0.102824

  10 0.806400

  11 0.950502

  12 0.735310

  dtype: float64

  s3 0.42803253918

  4. Series取頭和尾的值

  .head(n);.tail(n)

  取出頭n行或尾n行,n為可選參數(shù),若不填默認(rèn)5

  import pandas as pd

  import numpy as np

  v = np.random.random_sample(50)

  s = pd.Series(v)

  print("s.head()", s.head())

  print("s.head(3)", s.head(3))

  print("s.tail()", s.tail())

  print("s.head(3)", s.head(3))

  s.head() 0 0.714136

  1 0.333600

  2 0.683784

  3 0.044002

  4 0.147745

  dtype: float64

  s.head(3) 0 0.714136

  1 0.333600

  2 0.683784

  dtype: float64

  s.tail() 45 0.779509

  46 0.778341

  47 0.331999

  48 0.444811

  49 0.028520

  dtype: float64

  s.head(3) 0 0.714136

  1 0.333600

  2 0.683784

  dtype: float64

  5. Series常用操作

  import pandas as pd

  import numpy as np

  v = [10, 3, 2, 2, np.nan]

  v = pd.Series(v)

  print("len():", len(v)) # Series長度,包括NaN

  print("shape():", np.shape(v)) # 矩陣形狀,(,)

  print("count():", v.count()) # Series長度,不包括NaN

  print("unique():", v.unique()) # 出現(xiàn)不重復(fù)values值

  print("value_counts():\n", v.value_counts()) # 統(tǒng)計(jì)value值出現(xiàn)次數(shù)

  len(): 5無錫人流醫(yī)院哪家好 http://www.wxbhnkyy120.com/

  shape(): (5,)

  count(): 4

  unique(): [ 10. 3. 2. nan]

  value_counts():

  2.0 2

  3.0 1

  10.0 1

  dtype: int64

  6. Series加法

  import pandas as pd

  import numpy as np

  v = [10, 3, 2, 2, np.nan]

  v = pd.Series(v)

  sum = v[1:3] + v[1:3]

  sum1 = v[1:4] + v[1:4]

  sum2 = v[1:3] + v[1:4]

  sum3 = v[:3] + v[1:]

  print("sum", sum)

  print("sum1", sum1)

  print("sum2", sum2)

  print("sum3", sum3)

  sum 1 6.0

  2 4.0

  dtype: float64

  sum1 1 6.0

  2 4.0

  3 4.0

  dtype: float64

  sum2 1 6.0

  2 4.0

  3 NaN

  dtype: float64

  sum3 0 NaN

  1 6.0

  2 4.0

  3 NaN

  4 NaN

  dtype: float64

  7. Series查找

  范圍查找

  import pandas as pd

  import numpy as np

  s = {"ton": 20, "mary": 18, "jack": 19, "jim": 22, "lj": 24, "car": None}

  sa = pd.Series(s, name="age")

  print(sa[sa>19])

  jim 22.0

  lj 24.0

  ton 20.0

  Name: age, dtype: float64

  中位數(shù)

  import pandas as pd

  import numpy as np

  s = {"ton": 20, "mary": 18, "jack": 19, "jim": 22, "lj": 24, "car": None}

  sa = pd.Series(s, name="age")

  print("sa.median()", sa.median())

  sa.median() 20.0

  8. Series賦值

  import pandas as pd

  import numpy as np

  s = {"ton": 20, "mary": 18, "jack": 19, "jim": 22, "lj": 24, "car": None}

  sa = pd.Series(s, name="age")

  print(s)

  print('----------------')

  sa['ton'] = 99

  print(sa)

  {'ton': 20, 'mary': 18, 'jack': 19, 'jim': 22, 'lj': 24, 'car': None}

  ----------------

  car NaN

  jack 19.0

  jim 22.0

  lj 24.0

  mary 18.0

  ton 99.0

  Name: age, dtype: float64

關(guān)于“pd.Series()函數(shù)怎么用”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI