您好,登錄后才能下訂單哦!
今天小編給大家分享一下Pandas中Series的屬性,方法,常用操作使用實(shí)例分析的相關(guān)知識(shí)點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識(shí),所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
包的引入:
import numpy as np import pandas as pd
s = pd.Series() print(s) print(type(s))
需要傳入一個(gè)列表序列
l = [1, 2, 3, 4] s = pd.Series(l) print(s) print('-'*20) print(type(s))
需要傳入一個(gè)元組序列
t = (1, 2, 3) s = pd.Series(t) print(s) print('-'*20) print(type(s))
需要傳入一個(gè)字典
m = {'zs': 12, 'ls': 23, 'ww': 22} s = pd.Series(m) print(s) print('-'*20) print(type(s))
需要傳入一個(gè) ndarray
ndarr = np.array([1, 2, 3]) s = pd.Series(ndarr) print(s) print('-'*20) print(type(s))
index:用于設(shè)置 Series 對(duì)象的索引
age = [12, 23, 22, 34] name = ['zs', 'ls', 'ww', 'zl'] s = pd.Series(age, index=name) print(s) print('-'*20) print(type(s))
num = 999 s = pd.Series(num, index=[1, 2, 3, 4]) print(s) print('-'*20) print(type(s))
ndarr = np.arange(0, 10, 2) s = pd.Series(5, index=ndarr) print(s) print('-'*20) print(type(s))
l = [11, 22, 33, 44] s = pd.Series(l) print(s) print('-'*20) ndarr = s.values print(ndarr) print('-'*20) print(type(ndarr))
d = {'zs': 12, 'ls': 23, 'ww': 35} s = pd.Series(d) print(s) print('-'*20) idx = s.index print(idx) print('-'*20) print(type(idx))
d = {'zs': 12, 'ls': 23, 'ww': 35} s = pd.Series(d) print(s) print('-'*20) print(s.dtype)
d = {'zs': 12, 'ls': 23, 'ww': 35} s = pd.Series(d) print(s) print('-'*20) print(s.size)
d = {'zs': 12, 'ls': 23, 'ww': 35} s1 = pd.Series(d) print(s1) print('-'*20) print(s1.ndim) l = [[1, 1], [2, 2], [3, 3]] s2 = pd.Series(l) print(s2) print('-'*20) print(s2.ndim)
d = {'zs': 12, 'ls': 23, 'ww': 35} s1 = pd.Series(d) print(s1) print('-'*20) print(s1.shape) print() l = [[1, 1], [2, 2], [3, 3]] s2 = pd.Series(l) print(s2) print('-'*20) print(s2.shape)
l = [12, 23, 24, 34] s = pd.Series(l, index=['zs', 'ls', 'ww', 'zl']) print(s) print() print(s.mean())
l1 = [12, 23, 24, 34] s1 = pd.Series(l, index=['zs', 'ls', 'ww', 'zl']) print(s1) print() print(s1.max()) print(s1.min()) print() l2 = ['ac', 'ca', 'cd', 'ab'] s2 = pd.Series(l2) print(s2) print() print(s2.max()) print(s2.min())
l1 = [12, 23, 24, 34] s1 = pd.Series(l, index=['zs', 'ls', 'ww', 'zl']) print(s1) print() # argmax() -- 最大值的數(shù)字索引 # idxmax() -- 最大值的標(biāo)簽索引 # 兩個(gè)都不支持字符串類型的數(shù)據(jù) print(s1.max(), s1.argmax(), s1.idxmax()) print(s1.min(), s1.argmin(), s1.idxmin())
l = [12, 23, 24, 34] s = pd.Series(l, index=['zs', 'ls', 'ww', 'zl']) print(s) print() print(s.median())
l = [12, 23, 24, 34] s = pd.Series(l, index=['zs', 'ls', 'ww', 'zl']) print(s) print() print(s.value_counts())
l = [12, 23, 24, 34] s = pd.Series(l, index=['zs', 'ls', 'ww', 'zl']) print(s) print() print(s.mode()) print() l = [12, 23, 24, 34, 34] s = pd.Series(l, index=['zs', 'ls', 'ww', 'zl', 'zq']) print(s) print() print(s.mode())
四分位數(shù):把數(shù)值從小到大排列并分成四等分,處于三個(gè)分割點(diǎn)位置的數(shù)值就是四分位數(shù)。
需要傳入一個(gè)列表,列表中的元素為要獲取的數(shù)的對(duì)應(yīng)位置
l = [1, 1, 2, 2, 3, 3, 4, 4] s = pd.Series(l) print(s) print() print(s.quantile([0, .25, .50, .75, 1]))
總體標(biāo)準(zhǔn)差是反映研究總體內(nèi)個(gè)體之間差異程度的一種統(tǒng)計(jì)指標(biāo)。
總體標(biāo)準(zhǔn)差計(jì)算公式:
由于總體標(biāo)準(zhǔn)差計(jì)算出來會(huì)偏小,所以采用 ( n − d d o f ) (n-ddof) (n−ddof)的方式適當(dāng)擴(kuò)大標(biāo)準(zhǔn)差,即樣本標(biāo)準(zhǔn)差。
樣本標(biāo)準(zhǔn)差計(jì)算公式:
l = [1, 1, 2, 2, 3, 3, 4, 4] s = pd.Series(l) print(s) print() # 總體標(biāo)準(zhǔn)差 print(s.std()) print() print(s.std(ddof=1)) print() # 樣本標(biāo)準(zhǔn)差 print(s.std(ddof=2))
l = [1, 1, 2, 2, 3, 3, 4, 4] s = pd.Series(l) print(s) print() print(s.describe())
ascending:True為升序(默認(rèn)),F(xiàn)alse為降序 3.10.1 升序
l = [4, 2, 1, 3] s = pd.Series(l) print(s) print() s = s.sort_values() print(s)
l = [4, 2, 1, 3] s = pd.Series(l) print(s) print() s = s.sort_values(ascending=False) print(s)
ascending:True為升序(默認(rèn)),F(xiàn)alse為降序
l = [12, 23, 24, 34] s = pd.Series(l, index=['zs', 'ls', 'ww', 'zl']) print(s) print() s = s.sort_index() print(s)
l = [12, 23, 24, 34] s = pd.Series(l, index=['zs', 'ls', 'ww', 'zl']) print(s) print() s = s.sort_index() print(s)
需要傳入一個(gè)函數(shù)參數(shù)
# x 為當(dāng)前遍歷到的元素 def func(x): if (x%2==0): return x+1 else: return x l = [12, 23, 24, 34] s = pd.Series(l, index=['zs', 'ls', 'ww', 'zl']) print(s) print() # 調(diào)用 apply 方法,會(huì)將 Series 中的每個(gè)元素帶入 func 函數(shù)中進(jìn)行處理 s = s.apply(func) print(s)
對(duì)象的前 x 個(gè)元素 需要傳入一個(gè)數(shù) x ,表示查看前 x 個(gè)元素,默認(rèn)為前5個(gè)
l = [12, 23, 24, 34] s = pd.Series(l, index=['zs', 'ls', 'ww', 'zl']) print(s) print() # head(x) 查看 Series 對(duì)象的前 x 個(gè)元素 print(s.head(2))
需要傳入一個(gè)數(shù) x ,表示查看后 x 個(gè)元素,默認(rèn)為后5個(gè)
l = [12, 23, 24, 34] s = pd.Series(l, index=['zs', 'ls', 'ww', 'zl']) print(s) print() # tail(x) 查看 Series 對(duì)象的后 x 個(gè)元素 print(s.tail(2))
l = [12, 23, 24, 34] s = pd.Series(l) print(s) print() print(s[0]) print() print(s[1:-2]) print() print(s[::2]) print() print(s[::-1])
l = [12, 23, 24, 34] s = pd.Series(l, index=['zs', 'ls', 'ww', 'zl']) print(s) print() print(s[0]) print() print(s[1:-2]) print() print(s[::2]) print() print(s[::-1])
l = [12, 23, 24, 34] s = pd.Series(l, index=['zs', 'ls', 'ww', 'zl']) print(s) print() print(s['zs']) print() # 自定義標(biāo)簽索引進(jìn)行切片包含開始與結(jié)束位置 print(s['ls':'zl']) print() print(s['zs':'zl':2]) print() # 注意切邊范圍的方向與步長(zhǎng)的方向 print(s['zl':'zs':-1])
l = [12, 23, 24, 34] s = pd.Series(l, index=['zs', 'ls', 'ww', 'zl']) print(s) print() idx = (s%2==0) print(idx) print() # 索引掩碼(也是一個(gè)數(shù)組) # 索引掩碼個(gè)數(shù)與原數(shù)組的個(gè)數(shù)一致,數(shù)組每個(gè)元素都與索引掩碼中的元素一一對(duì)應(yīng) # 數(shù)組每個(gè)元素都對(duì)應(yīng)著索引掩碼中的一個(gè)True或False # 只有索引掩碼中為True所對(duì)應(yīng)元素組中的元素才會(huì)被選中 print(s[idx])
l = [12, 23, 24, 34] s = pd.Series(l, index=['zs', 'ls', 'ww', 'zl']) print(s) print() # 選出指定索引對(duì)應(yīng)的元素 print(s[['zs', 'ww']]) print() print(s[[1, 2]])
傳入要?jiǎng)h除元素的標(biāo)簽索引
l = [12, 23, 24, 34] s = pd.Series(l, index=['zs', 'ls', 'ww', 'zl']) print(s) print() s.pop('ww') print(s)
傳入要?jiǎng)h除元素的標(biāo)簽索引
l = [12, 23, 24, 34] s = pd.Series(l, index=['zs', 'ls', 'ww', 'zl']) print(s) print() # drop() 會(huì)返回一個(gè)刪除元素后的新數(shù)組,不會(huì)對(duì)原數(shù)組進(jìn)行修改 s = s.drop('zs') print(s)
l = [12, 23, 24, 34] s = pd.Series(l, index=['zs', 'ls', 'ww', 'zl']) print(s) print() s['zs'] = 22 print(s)
l = [12, 23, 24, 34] s = pd.Series(l, index=['zs', 'ls', 'ww', 'zl']) print(s) print() s[1] = 22 print(s)
l = [12, 23, 24, 34] s = pd.Series(l, index=['zs', 'ls', 'ww', 'zl']) print(s) print() s['ll'] = 22 print(s)
需要傳入一個(gè)要添加到原 Series 對(duì)象的 Series 對(duì)象
l = [12, 23, 24, 34] s = pd.Series(l, index=['zs', 'ls', 'ww', 'zl']) print(s) print() # 可以添加已經(jīng)存在的索引及其值 s2 = pd.Series([11, 13], index=['zs', 'wd']) # append() 不會(huì)對(duì)原數(shù)組進(jìn)行修改 s = s.append(s2) print(s) print() print(s['zs'])
以上就是“Pandas中Series的屬性,方法,常用操作使用實(shí)例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會(huì)為大家更新不同的知識(shí),如果還想學(xué)習(xí)更多的知識(shí),請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。