資料分析之pandas庫 series物件

2022-03-04 20:07:57 字數 1681 閱讀 2141

series是pandas中最基本的物件,series類似一種一維陣列。

1.生成物件。建立索引並賦值。

s1=pd.series()

2.檢視索引和值。

s1=series([1,2,3,4],index=['

a','

b','

c','

d'])

s1執行結果:

a    1

b 2

c 3

d 4

dtype: int64

3.series有字典的功能。

'b' in s1

執行結果:

true

list(s1.iteritems())

執行結果:

[('a', 1), ('b', 2), ('c', 3), ('d', 4)]

dict=

s2=pd.series(dict)

s2執行結果:

red 1

black 2

green 3

pink 4

dtype: int64

4.series物件的內容和索引都有個name屬性。

s1.name="

word

"s1.index.name="

number"s1

執行結果:

number

a 1b 2c 3d 4name: word, dtype: int64

5.用pandas的isnull和nonull可檢測缺失資料。

s1.isnull()

執行結果:

number

a false

b false

c false

d false

name: word, dtype: bool

1.series物件的下標運算同時支援位置和標籤兩種方式。

print("

位置下標:

",s1[0])

print("

標籤下標:

",s1['a'

])執行結果:

位置下標: 1標籤下標: 1

2.series物件支援位置切片和標籤切片,但需要注意的是後者包括結束標籤。

s1[1:3]

執行結果:

number

b 2c 3name: word, dtype: int64

3.和ndarray陣列一樣,可以用位置列表、位置陣列來訪問元素,同樣地,標籤列表、標籤陣列也能訪問。

s1[[1,3,2]]

執行結果:

number

b 2d 4c 3name: word, dtype: int64

4.還可通過索引進行排序(字典中缺失的則用nan作為內容)。

s1.index=["

c","

b","

a","d"

]s1執行結果:

c 1b 2a 3d 4name: word, dtype: int64

資料分析之Pandas

from pandas import series,dataframe import pandas as pd import numpy as np states california ohio oregon texas year 2000,2001,2002,2003 value 35000,71...

資料分析之pandas

pandas是基於numpy構建的庫,擁有兩種資料結構 series和dataframe series 就是一維陣列 dataframe 是二維陣列series in 1 from pandas import series,dataframe in 2 import pandas as pd in ...

Pandas庫 資料分析處理庫

pandas 基於numpy開發,主要資料結構是 series 一維資料 與 dataframe 二維資料 主要功能 資料預處理 特徵提取get dummies 處理缺失資料 表示為 nan 插入或刪除 dataframe 等多維物件的列 繪圖 將dataframe按列繪圖plot 資料對齊 顯式地...