溫馨提示×

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

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

Pandas中Series的屬性,方法,常用操作使用實(shí)例分析

發(fā)布時(shí)間:2022-07-26 09:14:57 來源:億速云 閱讀:153 作者:iii 欄目:開發(fā)技術(shù)

今天小編給大家分享一下Pandas中Series的屬性,方法,常用操作使用實(shí)例分析的相關(guān)知識(shí)點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識(shí),所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。

包的引入:

import numpy as np
import pandas as pd

1. Series 對(duì)象的創(chuàng)建

1.1 創(chuàng)建一個(gè)空的 Series 對(duì)象

s = pd.Series()
print(s)
print(type(s))

Pandas中Series的屬性,方法,常用操作使用實(shí)例分析

1.2 通過列表創(chuàng)建一個(gè) Series 對(duì)象

需要傳入一個(gè)列表序列

l = [1, 2, 3, 4]
s = pd.Series(l)
print(s)
print('-'*20)
print(type(s))

Pandas中Series的屬性,方法,常用操作使用實(shí)例分析

1.3 通過元組創(chuàng)建一個(gè) Series 對(duì)象

需要傳入一個(gè)元組序列

t = (1, 2, 3)
s = pd.Series(t)
print(s)
print('-'*20)
print(type(s))

Pandas中Series的屬性,方法,常用操作使用實(shí)例分析

1.4 通過字典創(chuàng)建一個(gè) Series 對(duì)象

需要傳入一個(gè)字典

m = {'zs': 12, 'ls': 23, 'ww': 22}
s = pd.Series(m)
print(s)
print('-'*20)
print(type(s))

Pandas中Series的屬性,方法,常用操作使用實(shí)例分析

1.5 通過 ndarray 創(chuàng)建一個(gè) Series 對(duì)象

需要傳入一個(gè) ndarray

ndarr = np.array([1, 2, 3])
s = pd.Series(ndarr)
print(s)
print('-'*20)
print(type(s))

Pandas中Series的屬性,方法,常用操作使用實(shí)例分析

1.6 創(chuàng)建 Series 對(duì)象時(shí)指定索引

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))

Pandas中Series的屬性,方法,常用操作使用實(shí)例分析

1.7 通過一個(gè)標(biāo)量(數(shù))創(chuàng)建一個(gè) Series 對(duì)象

num = 999
s = pd.Series(num, index=[1, 2, 3, 4])
print(s)
print('-'*20)
print(type(s))

Pandas中Series的屬性,方法,常用操作使用實(shí)例分析

ndarr = np.arange(0, 10, 2)
s = pd.Series(5, index=ndarr)
print(s)
print('-'*20)
print(type(s))

Pandas中Series的屬性,方法,常用操作使用實(shí)例分析

2. Series 的屬性

2.1 values ---- 返回一個(gè) ndarray 數(shù)組

l = [11, 22, 33, 44]
s = pd.Series(l)
print(s)
print('-'*20)
ndarr = s.values
print(ndarr)
print('-'*20)
print(type(ndarr))

Pandas中Series的屬性,方法,常用操作使用實(shí)例分析

2.2 index ---- 返回 Series 的索引序列

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))

Pandas中Series的屬性,方法,常用操作使用實(shí)例分析

2.3 dtype ---- 返回 Series 中元素的數(shù)據(jù)類型

d = {'zs': 12, 'ls': 23, 'ww': 35}
s = pd.Series(d)
print(s)
print('-'*20)
print(s.dtype)

Pandas中Series的屬性,方法,常用操作使用實(shí)例分析

2. 4 size ---- 返回 Series 中元素的個(gè)數(shù)

d = {'zs': 12, 'ls': 23, 'ww': 35}
s = pd.Series(d)
print(s)
print('-'*20)
print(s.size)

Pandas中Series的屬性,方法,常用操作使用實(shí)例分析

2.5 ndim ---- 返回 Series 的維數(shù)

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)

Pandas中Series的屬性,方法,常用操作使用實(shí)例分析

2.6 shape ---- 返回 Series 的維度

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)

Pandas中Series的屬性,方法,常用操作使用實(shí)例分析

3. Series 的方法

3.1 mean() ---- 求算術(shù)平均數(shù)

l = [12, 23, 24, 34]
s = pd.Series(l, index=['zs', 'ls', 'ww', 'zl'])
print(s)
print()
print(s.mean())

Pandas中Series的屬性,方法,常用操作使用實(shí)例分析

3.2 min() max() ---- 求最值

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())

Pandas中Series的屬性,方法,常用操作使用實(shí)例分析

3.3 argmax() argmin() idxmax() idxmin() ---- 獲取最值索引

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())

Pandas中Series的屬性,方法,常用操作使用實(shí)例分析

3.4 median() ---- 求中位數(shù)

l = [12, 23, 24, 34]
s = pd.Series(l, index=['zs', 'ls', 'ww', 'zl'])
print(s)
print()
print(s.median())

Pandas中Series的屬性,方法,常用操作使用實(shí)例分析

3.5 value_counts() ---- 求頻數(shù)

l = [12, 23, 24, 34]
s = pd.Series(l, index=['zs', 'ls', 'ww', 'zl'])
print(s)
print()
print(s.value_counts())

Pandas中Series的屬性,方法,常用操作使用實(shí)例分析

3.6 mode() ---- 求眾數(shù)

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())

Pandas中Series的屬性,方法,常用操作使用實(shí)例分析

3.7 quantile() ---- 求四分位數(shù)

四分位數(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]))

Pandas中Series的屬性,方法,常用操作使用實(shí)例分析

3.8 std() ---- 標(biāo)準(zhǔn)差

總體標(biāo)準(zhǔn)差是反映研究總體內(nèi)個(gè)體之間差異程度的一種統(tǒng)計(jì)指標(biāo)。
總體標(biāo)準(zhǔn)差計(jì)算公式:

Pandas中Series的屬性,方法,常用操作使用實(shí)例分析

由于總體標(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ì)算公式:

Pandas中Series的屬性,方法,常用操作使用實(shí)例分析

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))

Pandas中Series的屬性,方法,常用操作使用實(shí)例分析

3.9 describe() ---- 統(tǒng)計(jì) Series 的常見統(tǒng)計(jì)學(xué)指標(biāo)結(jié)果

l = [1, 1, 2, 2, 3, 3, 4, 4]
s = pd.Series(l)
print(s)
print()
print(s.describe())

Pandas中Series的屬性,方法,常用操作使用實(shí)例分析

3.10 sort_values() ---- 根據(jù)元素值進(jìn)行排序

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)

Pandas中Series的屬性,方法,常用操作使用實(shí)例分析

3.10.2 降序
l = [4, 2, 1, 3]
s = pd.Series(l)
print(s)
print()
s = s.sort_values(ascending=False)
print(s)

Pandas中Series的屬性,方法,常用操作使用實(shí)例分析

3.11 sort_index() ---- 根據(jù)索引值進(jìn)行排序

ascending:True為升序(默認(rèn)),F(xiàn)alse為降序

3.11.2 升序
l = [12, 23, 24, 34]
s = pd.Series(l, index=['zs', 'ls', 'ww', 'zl'])
print(s)
print()
s = s.sort_index()
print(s)

Pandas中Series的屬性,方法,常用操作使用實(shí)例分析

3.11.2 降序
l = [12, 23, 24, 34]
s = pd.Series(l, index=['zs', 'ls', 'ww', 'zl'])
print(s)
print()
s = s.sort_index()
print(s)

Pandas中Series的屬性,方法,常用操作使用實(shí)例分析

3.12 apply() ---- 根據(jù)傳入的函數(shù)參數(shù)處理 Series 對(duì)象

需要傳入一個(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)

Pandas中Series的屬性,方法,常用操作使用實(shí)例分析

3.13 head() ---- 查看 Series

對(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))

Pandas中Series的屬性,方法,常用操作使用實(shí)例分析

3.14 tail() ---- 查看 Series 對(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()
# tail(x) 查看 Series 對(duì)象的后 x 個(gè)元素
print(s.tail(2))

Pandas中Series的屬性,方法,常用操作使用實(shí)例分析

4. Series 的常用操作

4.1 Series 對(duì)象的數(shù)據(jù)訪問

4.1.1 使用數(shù)字索引進(jìn)行訪問
4.1.1.1 未自定義索引
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])

Pandas中Series的屬性,方法,常用操作使用實(shí)例分析

4.1.1.2 自定義索引
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])

Pandas中Series的屬性,方法,常用操作使用實(shí)例分析

4.1.2 使用自定義標(biāo)簽索引進(jìn)行訪問
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])

Pandas中Series的屬性,方法,常用操作使用實(shí)例分析

4.1.3 使用索引掩碼進(jìn)行訪問
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])

Pandas中Series的屬性,方法,常用操作使用實(shí)例分析

4.1.4 一次性訪問多個(gè)元素
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]])

Pandas中Series的屬性,方法,常用操作使用實(shí)例分析

4.2 Series 對(duì)象數(shù)據(jù)元素的刪除

4.2.1 pop()

傳入要?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)

Pandas中Series的屬性,方法,常用操作使用實(shí)例分析

4.2.2 drop()

傳入要?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)

Pandas中Series的屬性,方法,常用操作使用實(shí)例分析

4.3 Series 對(duì)象數(shù)據(jù)元素的修改

4.3.1 通過標(biāo)簽索引進(jìn)行修改
l = [12, 23, 24, 34]
s = pd.Series(l, index=['zs', 'ls', 'ww', 'zl'])
print(s)
print()
s['zs'] = 22
print(s)

Pandas中Series的屬性,方法,常用操作使用實(shí)例分析

4.3.2 通過數(shù)字索引進(jìn)行修改
l = [12, 23, 24, 34]
s = pd.Series(l, index=['zs', 'ls', 'ww', 'zl'])
print(s)
print()
s[1] = 22
print(s)

Pandas中Series的屬性,方法,常用操作使用實(shí)例分析

4.4 Series 對(duì)象數(shù)據(jù)元素的添加

4.4.1 通過標(biāo)簽索引添加
l = [12, 23, 24, 34]
s = pd.Series(l, index=['zs', 'ls', 'ww', 'zl'])
print(s)
print()
s['ll'] = 22
print(s)

Pandas中Series的屬性,方法,常用操作使用實(shí)例分析

4.4.2 append()

需要傳入一個(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í)例分析

以上就是“Pandas中Series的屬性,方法,常用操作使用實(shí)例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會(huì)為大家更新不同的知識(shí),如果還想學(xué)習(xí)更多的知識(shí),請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

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

免責(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)容。

AI