您當前的位置:首頁 > 書法

Pandas庫之Series使用介紹

作者:由 早睡覺多運動233 發表于 書法時間:2020-08-04

Pandas庫

pandas 是基於NumPy 的資料分析包,Pandas 的常用資料結構是 Series(一維資料)與 DataFrame(二維資料)

import pandas as pd

Pandas庫之series

Series是帶標籤的一維陣列,可儲存整數、浮點數、字串、Python物件型別的資料。軸標籤統稱為

索引

建立Series

Series屬性

增刪改

切片

索引

布林索引

修改index

資料型別轉換

資料結構轉換

統計函式

計數函式

排序

檢視資料缺失

資料填充

資料重複

逐元素操作

繪圖舉例

建立Series

呼叫

pd。Series

函式即可建立Series:

s

=

pd

Series

data

index

=

index

data支援三種資料結構

1。 字典

x

=

pd

Series

({

‘a’

1

‘b’

2

})

x

a

1

b

2

2。 列表

pd。Series([1,2,3],index=[‘a’,‘b’,‘c’])

a 1

b 2

c 3

3。 標量

pd。Series(1)

0 1

Series屬性

Series的value值是numpy中的陣列型別

x

=

pd

Series

([

1

2

3

])

x

dtype

dtype

‘int64’

x

index

#index預設從0開始

RangeIndex

strat

=

0

stop

=

3

step

=

1

x

values

array

([

1

2

3

],

dtype

=

int64

x

size

3

pd

Series

([

1

2

3

])

shape

3

,)

增刪改

x

=

pd

Series

([

1

2

3

])

x

0

1

1

2

2

3

dtype

int64

增加元素

x

3

=

4

x

0

1

1

2

2

3

3

4

dtype

int64

2。 刪除元素

x

drop

2

#不改變原來的x,需要賦值

0

1

1

2

3

4

dtype

int64

3。 修改元素

x

0

=

5

x

0

5

1

2

2

3

3

4

dtype

int64

切片

Series切片是對index進行切片,然後選出對應的值

x

=

pd

Series

range

1

6

))

x

0

1

1

2

2

3

3

4

4

5

dtype

int64

x

2

4

2

3

3

4

dtype

int64

2。 當index是字串時,切片時包含右端點

x

=

pd

Series

([

1

2

3

4

],

index

=

‘b’

‘d’

‘c’

‘a’

])

x

‘d’

‘a’

d

2

c

3

a

4

dtype

int64

索引

x

=

pd

Series

([

1

2

3

4

5

],

index

=

‘b’

‘d’

‘c’

‘a’

‘e’

])

x

b

1

d

2

c

3

a

4

e

5

dtype

int64

x

[[

‘a’

‘c’

]]

a

4

c

3

dtype

int64

布林索引

x

=

pd

Series

([

1

2

3

np

NaN

5

],

index

=

‘b’

‘d’

‘c’

‘a’

‘e’

])

x

b

1。0

d

2。0

c

3。0

a

NaN

e

5。0

dtype

float64

print

x

>

2

#x>2 對於值來說

b

False

d

False

c

True

a

False

e

True

dtype

bool

x

x

>

2

#按值索引

c

3。0

e

5。0

dtype

float64

修改index

x

=

pd

Series

([

i

for

i

in

range

3

)],

index

=

‘a’

‘b’

‘c’

])

print

x

a

0

b

1

c

2

dtype

int64

x

index

=

‘c’

‘d’

‘e’

print

x

c

0

d

1

e

2

dtype

int64

x

=

x

reset_index

drop

=

True

#drop=True刪除原有索印列

print

x

0

0

1

1

2

2

dtype

int64

資料型別轉換

astype

x

=

pd

Series

([

1

2

3

])

print

x

0

1

1

2

2

3

dtype

int64

x

=

x

astype

np

float

print

x

0

1。0

1

2。0

2

3。0

dtype

float64

資料結構轉換

Series->ndarray/list/dict/frame

x

=

pd

Series

([

1

2

3

])

print

x

values

1

2

3

print

x

to_numpy

())

1

2

3

print

x

to_list

())

1

2

3

print

x

to_dict

())

{

0

1

1

2

2

3

}

print

x

to_frame

())

0

0

1

1

2

2

3

統計函式

x

=

pd

Series

([

1

2

2

3

3

4

])

print

x

mode

())

#出現次數最多的值,可以是多個值,返回值為Series型別

0

2

1

3

dtype

int64

print

x

max

())

4

print

x

mean

())

2。5

print

x

median

())

1。5

print

x

std

())

1。0488088481701516

計數函式

x

=

pd

Series

([

1

2

2

3

3

4

])

print

x

count

())

#統計非空元素個數

6

print

x

value_counts

())

#統計每個元素個數,預設降序排列,ascending=True則升序排列

3

2

2

2

4

1

1

1

dtype

int64

排序

x

=

pd

Series

([

1

2

3

4

],

index

=

‘b’

‘a’

‘c’

‘c’

])

print

x

b

1

a

2

c

3

c

4

dtype

int64

print

x

sort_index

ascending

=

False

))

c

3

c

4

b

1

a

2

dtype

int64

print

x

sort_values

())

b

1

a

2

c

3

c

4

dtype

int64

檢視資料缺失

x

=

pd

Series

([

1

2

np

NaN

3

])

print

x

x

isnull

()])

2

NaN

dtype

float64

缺失填充

method=‘None’

用一個指定值去填充缺失值(預設預設這種方式)

x

=

pd

Series

([

1

2

np

NaN

3

])

print

x

fillna

9

))

0

1。0

1

2。0

2

9。0

3

3。0

dtype

float64

2。

method=pad/ffill

前一個

非缺失值去填充該缺失值

print

x

fillna

method

=

‘ffill’

))

0

1。0

1

2。0

2

2。0

3

3。0

dtype

float64

3。

method=backfill/bfill

下一個

非缺失值填充該缺失值

print

x

fillna

method

=

‘backfill’

))

0

1。0

1

2。0

2

3。0

3

3。0

dtype

float64

資料重複

x

=

pd

Series

([

1

2

2

3

3

4

])

print

x

unique

())

1

2

3

4

print

x

x

duplicated

()])

2

2

4

3

dtype

int64

print

x

~

x

duplicated

()])

0

1

1

2

3

3

5

4

dtype

int64

print

x

drop_duplicates

keep

=

False

inplace

=

False

))

#keep=‘first’表示保留第一次出現的重複行,是預設值。

#“last”和False,分別表示保留最後一次出現的重複行和去除所有重複行。

#inplace=True表示直接在原來的DataFrame上刪除重複項,而預設值False表示生成一個副本

0

1

5

4

dtype

int64

逐元素操作

apply(func,convert_dtype=True,args=(),**kwds)

x

=

pd

Series

([

1

2

3

])

def

num_nap

x

bias

):

return

x

+

bias

print

x

apply

num_nap

args

=

3

,)))

0

4

1

5

2

6

dtype

int64

繪圖舉例

Pandas可呼叫matplotlib繪圖,相比matplotlib,pandas繪圖簡單快捷,不需要很多引數,從而可以專注於資料分析

import

pandas

as

pd

import

numpy

as

np

from

matplotlib

import

pyplot

as

plt

x

=

pd

Series

([

1

1

2

2

4

])

counts

=

x

value_counts

()

print

counts

counts

plot

bar

()

plt

show

()

2

2

1

2

4

1

dtype

int64

Pandas庫之Series使用介紹

標簽: Series  Pd  print  index  int64