1. 程式人生 > >Pandas中Series用法總結

Pandas中Series用法總結

Series:帶標籤的陣列

Array/Series/DataFrame對比學習
本文對Pandas包中的一維資料型別Series特點及用法進行了總結歸納。
Pandas包之Series

2.1 如何建立Sereis

#匯入Pandas包
import pandas as pd

#建立Series
#1.1.1 通過列表List
listSer=pd.Series([10,20,30,40])
print(listSer)

#1.1.2 通過字典dict
dictSer=pd.Series({'a':10,'b':40,'c':5,'d':90,'e':35,'f':40},name='數值')
print(dictSer)

#1.1.3 通過array
import
numpy as np arrySer=pd.Series(np.arange(10,15),index=['a','b','c','d','e']) print(arrySer) [output] 0 10 1 20 2 30 3 40 dtype: int64 a 10 b 40 c 5 d 90 e 35 f 40 Name: 數值, dtype: int64 a 10 b 11 c 12 d 13 e 14 dtype: int64

2.2 索引及name屬性

Series型別包括(index,values)兩部分

#index
print(arrySer.index)
#values
print(arrySer.values)

[output]
Index(['a', 'b', 'c', 'd', 'e'],dtype='object')
[10 11 12 13 14]

2.3 獲取資料

#iloc通過位置獲取資料
dictSer[0:1] #相當於dictSer.iloc[0:1]

#loc通過索引獲取資料
dictSer[['a','b']]  #相當於dictSer.loc[['a','b']]

#boolean indexing獲取值
dictSer[dictSer.values<=
10] #獲取值不超過10的資料 dictSer[dictSer.index!='a'] #獲取索引值不是a的資料

2.4 基本運算

檢視描述性統計資料

dictSer.describe() 
dictSer.mean() #均值
dictSer.median() #中位數
dictSer.sum() #求和
dictSer.std() #標準差
dictSer.mode() #眾數
dictSer.value_counts() #每個值的數量

數學運算

dictSer/2 #對每個值除2
dictSer%2 #取餘
dictSer**2 #求平方
np.sqrt(dictSer) #求開方
np.log(dictSer) #求對數

對齊計算

dictSer2=pd.Series({'a':10,'b':20,'d':23,'g':90,'h':35,'i':40},name='數值')
dictSer3=dictSer+dictSer2
dictSer3

2.5 缺失值處理

#找出空/非空值
dictSer3[dictSer3.notnull()] #非空值
dictSer3[dictSer3.isnull()]  #空值
#填充空值
dictSer3=dictSer3.fillna(dictSer3.mean()) #用均值來填充缺失值

2.6 刪除值

dictSer3=dictSer3.drop('b')
print(dictSer3)

如果對於本文中程式碼或資料有任何疑問,歡迎評論或私信交流

相近文章:
Numpy中Array用法總結
Pandas中DataFrame用法總結