1. 程式人生 > >pandas中的資料物件Series

pandas中的資料物件Series

pandas 的資料物件 Series

概要

用pandas 有一段時間,很少去總結,這篇文章 簡單總結一些 pandas 中series 的一些常用方法,如果有更多的需要,可以查詢官方文件.

series官方文件

構造一個series 物件

import   numpy  as np
import  pandas as pd

s = pd.Series(np.arange(6),index=list("ABCDEF"))

Series 有兩部分組成

index 是索引物件,儲存標籤資訊
values 是儲存元素值的 ndarray陣列

s
A    0
B    1
C    2
D    3
E    4
F    5
dtype: int64
s.index 
Index(['A', 'B', 'C', 'D', 'E', 'F'], dtype='object')
s.values
array([0, 1, 2, 3, 4, 5])

series 可以通過 位置,或者標籤資訊 拿到資料,同時 也支援python中的切片操作。

1.通過位置來索引

s[0]
0
s[1]
1
s[3]
3
s[2:4]
C    2
D    3
dtype: int64
  1. 通過index 來取值
    如果通過index 去取值,切邊的話,兩端都包含。這一點和 普通切片有點區別的.
s
A    0
B    1
C    2
D    3
E    4
F    5
dtype: int64

s['D']
3
s['B']
1
s['C':'E']
C    2
D    3
E    4
dtype: int64

dtype: int64
s['C':'F']
C    2
D    3
E    4
F    5
dtype: int64
s['C':'G']
C    2
D    3
E    4
F    5
dtype: int64

如果構造series沒有指定index, 則自動生成 0到series長度-1 的Rangeindex

s2 = pd.Series([20,30,40,50])
s2
0    20
1    30
2    40
3    50
dtype: int64
s2.index
RangeIndex(start=0, stop=4, step=1)
s2.values
array([20, 30, 40, 50])

來說談一下series的操作

1 排序操作

sort_values

sort_values(self, axis=0, ascending=True, inplace=False,  kind='quicksort', na_position='last')

na_position : {‘first’ or ‘last’}, default ‘last’
inplace= False or  True 


sort_value 有幾個引數

  • ascending 是否升序 True or False
  • inplace 是否 原地替換, 預設False ,返回一個series 不改變原來的值,如果設定為True 直接改變series 的值。
  • na_position 空值的位置,放在前面還是後面,預設值是last,也可以是first
  • kind 是選擇什麼排序演算法,{‘quicksort’, ‘mergesort’ or ‘heapsort’} 預設是 quicksort
s = pd.Series([np.nan, 1, 3, 10,8,2, 5])

s = pd.Series([np.nan, 1, 3, 10, 5])



s = pd.Series([np.nan, 1, 3, 10, 5])

# 升序排序 預設
s.sort_values(ascending=True)
1     1.0
2     3.0
4     5.0


# 降序排序
s.sort_values(ascending=False)


# inplace  是否直接替換原來的series 的值 ,預設是False  
# 重新構造一個 series
s = pd.Series([np.nan, 1, 3, 10,8,2, 5])

s = pd.Series([np.nan, 1, 3, 10,8,2, 5])
s
0     NaN
1     1.0
2     3.0
3    10.0
4     8.0
5     2.0
6     5.0
dtype: float64
s.sort_values(inplace=True)
s
1     1.0
5     2.0
2     3.0
6     5.0
4     8.0
3    10.0
0     NaN
dtype: float64


s = pd.Series([np.nan, 1, 3, 10,8,2, 5])
s.sort_values()
1     1.0
5     2.0
2     3.0
6     5.0
4     8.0
3    10.0
0     NaN
dtype: float64


s.sort_values(na_position='first')
0     NaN
1     1.0
5     2.0
2     3.0
6     5.0
4     8.0
3    10.0
dtype: float64

  1. Series去重操作

drop_duplicates

方法1 :drop_duplicates

def drop_duplicates(self, keep='first', inplace=False): pass 
  keep : {‘first’, ‘last’, False}, default ‘first’
        ‘first’ : Drop duplicates except for the first occurrence.
        ‘last’ : Drop duplicates except for the last occurrence.
        False : Drop all duplicates.
 inplace : boolean, default False
If True, performs operation inplace and returns None.

簡單 解釋 一下,

drop_duplicates 刪除 重複的資料
keep 這個引數, 如果 有重複的資料 要保留哪一個 ,

  • first 保留第一個出現的,
  • last 保留最後一個出現的.
  • False 代表不保留直接刪除. (當然不是真的刪除,只是返回了一個series,如果要直接刪除需要和 inplace=True 結合使用)

inplace 引數 True, False 是否要原地刪除資料.

  1. 看一個例子
s = pd.Series(['lama', 'cow', 'frank','lama', 'frank','beetle', 'lama', 'hippo'], name='animal')


s
0      lama
1       cow
2     frank
3      lama
4     frank
5    beetle
6      lama
7     hippo
Name: animal, dtype: object

#保留第一個
s.drop_duplicates(keep='first')
0      lama
1       cow
2     frank
5    beetle
7     hippo
Name: animal, dtype: object
# 保留最後一個
s.drop_duplicates(keep='last')
1       cow
4     frank
5    beetle
6      lama
7     hippo
Name: animal, dtype: object

# 直接刪除 重複資料.
s.drop_duplicates(keep=False)
1       cow
5    beetle
7     hippo
Name: animal, dtype: object


# 當然 要想真的原地刪除,需要  inplace=True  這個選項 
s.drop_duplicates(keep=False,inplace=True)
s
1       cow
5    beetle
7     hippo
Name: animal, dtype: object

順便提下 這個方法 s.duplicated(keep=False)

用來判斷是否是重複資料,標記 為True or False

Series.duplicated(keep='first')[source]
Indicate duplicate Series values.
Duplicated values are indicated as True values in the resulting Series. Either all duplicates, all except the first or all except the last occurrence of duplicates can be indicated.

Parameters: keep : {‘first’, ‘last’, False}, default ‘first’
    ‘first’ : Mark duplicates as True except for the first occurrence.
    ‘last’ : Mark duplicates as True except for the last occurrence.
    False : Mark all duplicates as True.
Returns: pandas.core.series.Series


keep   有三個值 first   ,last ,False  

如果 keep  first  第一次出現是 False ,其他都是True 
    last  最後一次出現標記為 False  ,其他情況標記為True
   False     只要出現 就為 True  

s2 = pd.Series(['lama', 'cow', 'frank','lama', 'frank','beetle', 'lama', 'hippo'], name='animal')

s2
0      lama
1       cow
2     frank
3      lama
4     frank
5    beetle
6      lama
7     hippo
Name: animal, dtype: object

s2.duplicated(keep='first')
0    False
1    False
2    False
3     True   # 第二次出現
4     True   # 第二次出現
5    False
6     True   # 第三次出現
7    False 
Name: animal, dtype: bool

s2.duplicated(keep='last')
0     True #  lama  第一次出現
1    False
2     True
3     True #  lama 第二次出現
4    False 
5    False
6    False #  lama  第三次出現 ,也是最後一次出現
7    False
Name: animal, dtype: bool

# 只要重複出現就為 True 
s2.duplicated(keep=False)
0     True
1    False
2     True
3     True
4     True
5    False
6     True
7    False
Name: animal, dtype: bool

方法2:Series.unique 方法
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.unique.html#pandas.Series.unique

Series.unique()[source]
Return unique values of Series object.
Uniques are returned in order of appearance. Hash table-based unique, therefore does NOT sort.

Returns: ndarray or CategoricalThe unique values returned as a NumPy array. In case of categorical data type, returned as a Categorical.
該方法 直接就返回   array  保留一個 返回一個 array 物件

這個方法幾乎沒有什麼引數,直接返回一個去重的array  物件. 
The unique values returned as a NumPy array.
s = pd.Series([2, 1, 3, 3,4,1,2], name='number')

s.unique()
array([2, 1, 3, 4])
s
0    2
1    1
2    3
3    3
4    4
5    1
6    2
Name: number, dtype: int64

3.常用的一些對series 的操作

import pandas  as pd
import numpy as  np
s = pd.Series(data= range(10),name='numbers',index=[ chr(i) for i in range(97,107)])


# 獲取series的名稱
s.name
'numbers'

# 獲取所有的索引

print(s.index)

# 獲取所有的值
print(s.values)

# 判斷是否為nan 
print(s.isnull())

print(pd.isnull(s))

# 根據index 訪問值
# 列印所有的index
print(s.index)
print(s.get('b'))
print(s['b'])

# 一次訪問多個不連續的值.
# 傳入一個 list ,值是index  [index,index,index,]
print(s[['a', 'b', 'f', 'j']])


# 獲取series 的資料個數(Attributes)
print(s.size)
10

4 diff 函式

參考文件
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.diff.html#pandas.Series.diff

Calculates the difference of a Series element compared with another element in the Series (default is element in previous row).

Parameters: periods : int, default 1Periods to shift for calculating difference, accepts negative values.
Returns: diffed : Series

diff 預設是 後一個值 減去 前面一個值, 這樣第一個就是 NaN , 後面都會有結果 .
有periods 可以設定 怎麼減, 2 跨兩個減, 0 自己減自己. (當前index 對應的值相減)
-1 前面減去後面一個值 . (就是index小的減去index 大的.

values 裡面 後面減去前面的值,如果沒有就是NaN , periods 預設是1 ,如果 是2 就是跨兩個數相減.

s = pd.Series([1, 1, 2, 3, 5, 8])
s = pd.Series([1, 1, 2, 3, 5, 8])
s.diff()
0    NaN
1    0.0
2    1.0
3    1.0
4    2.0
5    3.0
dtype: float64
s.diff(3)
0    NaN
1    NaN
2    NaN
3    2.0    #  3-1 
4    4.0    #  5-1 
5    6.0    #  8-2 
dtype: float64
s
0    1
1    1
2    2
3    3
4    5
5    8
dtype: int64
s.diff()
0    NaN
1    0.0
2    1.0
3    1.0
4    2.0
5    3.0
dtype: float64
s.diff(2)
0    NaN
1    NaN
2    1.0
3    2.0
4    3.0
5    5.0
dtype: float64
s
0    1
1    1
2    2
3    3
4    5
5    8
dtype: int64
s.diff(periods=2)
0    NaN
1    NaN
2    1.0
3    2.0
4    3.0
5    5.0
dtype: float64
s.diff(periods=-1)
0    0.0
1   -1.0
2   -1.0
3   -2.0
4   -3.0
5    NaN
dtype: float64
s.diff(periods=-2)
0   -1.0
1   -2.0
2   -3.0
3   -5.0
4    NaN
5    NaN
dtype: float64

# 自己減自己肯定是0 
s.diff(0)
0    0.0
1    0.0
2    0.0
3    0.0
4    0.0
5    0.0
dtype: float64
  1. series 如何判斷是否為空值

斷是否是缺失 值

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.isna.html#pandas.Series.isna

# 判斷是否為None  , 如果為 None,返回 True 
series.isnull() 
series.isna() 


# 判斷是否不為None , 如果不為None ,返回 True 
series.notnull()
series.notna()

s= pd.Series([1, 8,10, np.NaN])
s
0     1.0
1     8.0
2    10.0
3     NaN
dtype: float64

# 是否為None
s.isna()
0    False
1    False
2    False
3     True
dtype: bool
s.isnull()
0    False
1    False
2    False
3     True
dtype: bool

# 是否不為None 
s.notnull()
0     True
1     True
2     True
3    False
dtype: bool
s.notna()
0     True
1     True
2     True
3    False
dtype: bool


6 series.tolist() 方法, 返回一個list 根據values 的值,返回 一個list

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.tolist.html#pandas.Series.tolist
s.tolist()

Return a list of the values.

通過 series 轉成 python 的list 物件

s= pd.Series([1, 8,10, np.NaN]) 


s.tolist()
[1.0, 8.0, 10.0, nan]
s
0     1.0
1     8.0
2    10.0
3     NaN
dtype: float64
s.tolist()
[1.0, 8.0, 10.0, nan]


s= pd.Series(['laoda','laoer','laosan'],name='person') 
s
0     laoda
1     laoer
2    laosan
Name: person, dtype: object
s.tolist()
['laoda', 'laoer', 'laosan']
總結

本文總結了series常用的操作,Series 做為pandas 的核心物件之一,其實有很多方法 和dataframe 是差不多的。更多的瞭解還請查詢 官方文件,裡面有詳細的介紹.

參考文件

Series index 官方首頁 https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.html

分享快樂,留住感動.2018-12-01 16:03:11 --frank