Series (帶有標簽的一維數組)
一、Series的創建方法
[注]import numpy as np import pandas as pd 都省略了。
1、通過字典創建
dic = {'a':1,'b':2,'c':3,'1':'hello','2':'python','3':[1,2]} s = pd.Series(dic) print(s,type(s)) #運行結果 1 hello 2 python 3 [1, 2] a 1 b 2 c 3 dtype: object <class 'pandas.core.series.Series'>
2、通過數組(ndarray)創建
參數index:是Series的標簽
參數name: 是Series的名稱,沒有的話默認為None。可以用rename()來更改,更改后生成新的Series,不改變原來的Series
s = pd.Series(np.random.rand(5),index = list('abcde'),name = 'test') print(s,type(s)) #運行結果 a 0.384840 b 0.202776 c 0.646176 d 0.215777 e 0.605895 Name: test, dtype: float64 <class 'pandas.core.series.Series'> #rename()的用法 s1 = s.rename('excel') print(s1) #運行結果 a 0.499740 b 0.943519 c 0.643355 d 0.591372 e 0.418790 Name: excel, dtype: float64
3、通過標量創建(必須聲明index值)
s = pd.Series(5,index = range(5)) print(s) #運行結果 0 5 1 5 2 5 3 5 4 5 dtype: int64
二、Series的索引
1、位置下標索引
s = pd.Series(np.random.rand(5)) print(s,'\n') print(s[2],type(s[2]),s[2].dtype) #運行結果 0 0.961192 1 0.394670 2 0.948766 3 0.658049 4 0.214219 dtype: float64 0.948766189751 <class 'numpy.float64'> float64
2、標簽(index)索引
當有多個標簽要索引的話,要多加個[ ]
s = pd.Series(np.random.rand(5),index = list('abcde')) print(s,'\n') print(s['a'],'\n') print(s[['a','b']]) #運行結果 a 0.593557 b 0.991561 c 0.611022 d 0.603023 e 0.518528 dtype: float64 0.593556973009 a 0.593557 b 0.991561 dtype: float64
3、切片索引
s1 = pd.Series(np.random.rand(5),list('abcde')) print(s1,'\n') print(s1['a':'b'],'\n') #用index做索引的話是末端包含的 print(s1[1:2],'\n') #用下標做切片索引的話和list切片是一樣的,不包含末端 #運行結果 a 0.973470 b 0.192143 c 0.805640 d 0.623555 e 0.040572 dtype: float64 a 0.973470 b 0.192143 dtype: float64 b 0.192143 dtype: float64
4、布爾型索引
布爾型索引判斷,生成的是一個由布爾型組成的新的Series。
.isnull()和.notnull() 判斷是否是空值,其中None表示空值,NaN表示有問題的值,兩個都會被判斷為空值。
s = pd.Series(np.random.rand(2)*100) s[3] = None # 添加一個空值 print(s,'\n') bs1 = s > 50 bs2 = s.isnull() bs3 = s.notnull() print(bs1,bs2,bs3,'\n') print(s[s > 50]) #運行結果 0 71.1548 1 53.9334 3 None dtype: object 0 True 1 True 3 False dtype: bool 0 False 1 False 3 True dtype: bool 0 True 1 True 3 False dtype: bool 0 71.1548 1 53.9334 dtype: object
二、Series 的基本操作用法
1、增添(第一,直接下標索引或index添加;第二,通過append()添加,生成新的Series)
s = pd.Series(np.random.rand(2)) s[3]= 100 #用index增添 s['a'] = 200 print(s,'\n') #運行結果 0 0.646847 1 0.224802 3 100.000000 a 200.000000 dtype: float64 s2 = pd.Series(np.random.rand(2),index = ['value1','value2']) s3 = s.append(s2) #用append()增添 print(s3) #運行結果 0 0.646847 1 0.224802 3 100.000000 a 200.000000 value1 0.225087 value2 0.504572 dtype: float64
2、刪除(第一,用del刪除;第二,用.drop()刪除,會生成新的Series)
s = pd.Series(np.random.rand(5),index = list('abcde')) del s['a'] #用del刪除 print(s,'\n') #運行結果 b 0.036584 c 0.319169 d 0.267866 e 0.855804 dtype: float64 s1 = s.drop(['c','d']) #用.drop()刪除,刪除多個要加[] print(s1) #運行結果 b 0.036584 e 0.855804 dtype: float64
3、修改(通過索引直接修改)
s = pd.Series(np.random.rand(5),index = list('abcde')) print(s,'\n') s[1] = 100 print(s,'\n') s[['c','d']] = 200 print(s) #運行結果 a 0.900485 b 0.955717 c 0.270206 d 0.186294 e 0.503710 dtype: float64 a 0.900485 b 100.000000 c 0.270206 d 0.186294 e 0.503710 dtype: float64 a 0.900485 b 100.000000 c 200.000000 d 200.000000 e 0.503710 dtype: float64
4、數據查看
.head()方法是查看前幾行的數據,默認是5行
.tail()方法是查看后幾行的數據,默認也是5行
s = pd.Series(np.random.rand(10)) print(s.head(2),'\n') print(s.tail()) #運行結果 0 0.301042 1 0.344857 dtype: float64 5 0.461262 6 0.337744 7 0.215328 8 0.735952 9 0.066285 dtype: float64
5、重新索引
.reindex(新的標簽,fill_value = )會根據更改后的標簽重新排序,若添加了原標簽中沒有的新標簽,則默認填入NaN,參數fill_value指對新出現的標簽填入的值。
s = pd.Series(np.random.rand(3), index = ['a','b','c']) s1 = s.reindex(['c','b','a','A'],fill_value = 100) print(s1) #運行結果 c 0.223246 b 0.484170 a 0.844028 A 100.000000 dtype: float64
6、對齊
s1 = pd.Series(np.random.rand(3), index = ['a','b','c']) s2 = pd.Series(np.random.rand(3), index =['a','c','A']) print(s1,'\n') print(s2,'\n') print(s1+s2) #運行結果 a 0.168003 b 0.785439 c 0.218607 dtype: float64 a 0.860634 c 0.233973 A 0.691131 dtype: float64 A NaN a 1.028637 b NaN c 0.452580 dtype: float64
空值和任何值相加都會返回空值。