Pandas詳解一之Series對象

約定:

import pandas as pd
from pandas import Series,DataFrame
import numpy as np

Series

一、Series屬性及方法

  • Series是Pandas中最基本的對象,Series類似一種一維數組:
se1=Series([4,7,-2,8])
se1

代碼結果:

0    4
1    7
2   -2
3    8
dtype: int64
  • 通常,我們希望能自己創建索引:
se2=Series([4,7,-2,8],index=['b','c','a','d'])
se2

代碼結果:

b    4
c    7
a   -2
d    8
dtype: int64
  • 可通過Series的倆個屬性valuesindex獲取內容和索引:
se1.values

代碼結果:

array([ 4,  7, -2,  8], dtype=int64)
se1.index

代碼結果:

RangeIndex(start=0, stop=4, step=1)
  • Series對象有字典的功能
'b' in se2

代碼結果:

True
list(se2)

代碼結果:

[4, 7, -2, 8]
list(se2.iteritems())

代碼結果:

[('b', 4), ('c', 7), ('a', -2), ('d', 8)]
  • 還可將字典轉換爲Series:
dict={"red":100,"black":400,"green":300,"pink":900}
se3=Series(dict)
se3

代碼結果:

black    400
green    300
pink     900
red      100
dtype: int64
  • Series對象的內容和索引都有個name屬性:
se3

代碼結果:

black    400
green    300
pink     900
red      100
dtype: int64
se3.name="values"
se3.index.name="color"
se3

代碼結果:

color
black    400
green    300
pink     900
red      100
Name: values, dtype: int64
  • 用pandas的isnullnonull可檢測缺失數據:
pd.isnull(se3)

代碼結果:

purple    False
brown     False
glod      False
blue      False
Name: values, dtype: bool
pd.notnull(se3)

代碼結果:

purple    True
brown     True
glod      True
blue      True
Name: values, dtype: bool
  • 或者直接用Series對象(se4)的 isnull
se3.isnull()

代碼結果:

purple    False
brown     False
glod      False
blue      False
Name: values, dtype: bool

二、Series對象存取

  • Series對象的下標運算同時支持位置標籤兩種方式:
print("位置下標:  ",se2[1])
print("標籤下標:  ",se2['c'])

代碼結果:

位置下標:   7
標籤下標:   7
  • Series對象支持位置切片標籤切片,但需要注意的是後者包括結束標籤:
se2

代碼結果:

b    4
c    7
a   -2
d    8
dtype: int64
se2[1:3]

代碼結果:

c    7
a   -2
dtype: int64
se2['b':'a']

代碼結果:

b    4
c    7
a   -2
dtype: int64
  • 和ndarray數組一樣,可以用位置列表、位置數組來存取元素,同樣地,標籤列表、標籤數組也能存取:
se2[[1,3,2]]

代碼結果:

c    7
d    8
a   -2
dtype: int64
se2[['a','b','c']]

代碼結果:

a   -2
b    4
c    7
dtype: int64
  • 還可通過索引進行排序(字典中缺失的則用NaN作爲內容):
se4=Series(dict,index=["red","yellow","green","white","black","pink"])
se4

代碼結果:

red       100.0
yellow      NaN
green     300.0
white       NaN
black     400.0
pink      900.0
dtype: float64
  • Series對象的內容可通過索引賦值進行重新排序
se4.index=["red","yellow","black","pink","green","white"]
se4

代碼結果:

red       100.0
yellow      NaN
black     300.0
pink        NaN
green     400.0
white     900.0
dtype: float64

三、Series運算特性

  • 支持Numpy數組運算(布爾數組過濾、標量乘法、數學函數):
se2

代碼結果:

b    4
c    7
a   -2
d    8
dtype: int64
se2[se2>0]

代碼結果:

b    4
c    7
d    8
dtype: int64
se2*2

代碼結果:

b     8
c    14
a    -4
d    16
dtype: int64
np.exp(se2)

代碼結果:

b      54.598150
c    1096.633158
a       0.135335
d    2980.957987
dtype: float64
  • 兩個Series對象支持操作符運算,Series會按照標籤對齊元素再運算(也就是隻有相同標籤的元素才能進行運算),當某一方標籤不存在則默認用NaN填充:
se2+se3

代碼結果:

a        NaN
b        NaN
blue     NaN
brown    NaN
c        NaN
d        NaN
glod     NaN
purple   NaN
dtype: float64

謝謝大家的瀏覽,
希望我的努力對您有幫助,
共勉!

發佈了48 篇原創文章 · 獲贊 181 · 訪問量 34萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章