Series
構造Series
obj=Series([4,5,-7,7])obj Out[140]: 0 4 1 5 2 -7 3 7 dtype: int64obj.index Out[141]: RangeIndex(start=0, stop=4, step=1)obj.values Out[142]: array([ 4, 5, -7, 7], dtype=int64)構造同時指定索引
obj2=Series([4,5,-7,7],index=['a','b','c','d'])obj2 Out[144]: a 4 b 5 c -7 d 7 dtype: int64讀取
obj2.a Out[148]: 4obj2['a'] Out[149]: 4數學運算
obj2[obj2>0] Out[150]: a 4 b 5 d 7 dtype: int64obj2*2 Out[151]: a 8 b 10 c -14 d 14 dtype: int64np.exp(obj2) Out[152]: a 54.598150 b 148.413159 c 0.000912 d 1096.633158 dtype: float64 'b' in obj2 Out[153]: True't' in obj2 Out[154]: False由字典變成Series
sdata={'ohio':3500,'texas':71000,'oregon':16000,'utah':500}obj3=Series(sdata)obj3 Out[159]: ohio 3500 oregon 16000 texas 71000 utah 500 dtype: int64替換字典的index,與California 對應的sdata 沒有,顯示是NaN
states=['california','ohio','oregon','texas']sdata={'ohio':3500,'texas':71000,'oregon':16000,'utah':500}obj4=Series(sdata,index=states)obj4 Out[170]: california NaN ohio 3500.0 oregon 16000.0 texas 71000.0 dtype: float64判斷數據是否缺失
pd.isnull(obj4) Out[171]: california True ohio False oregon False texas False dtype: boolpd.notnull(obj4) Out[172]: california False ohio True oregon True texas True dtype: bool判斷缺失的另一種表達
obj4.notnull() Out[176]: california False ohio True oregon True texas True dtype: boolobj4.isnull() Out[177]: california True ohio False oregon False texas False dtype: bool obj3 Out[18]: ohio 3500 oregon 16000 texas 71000 utah 500 dtype: int64obj4 Out[19]: california NaN ohio 3500.0 oregon 16000.0 texas 71000.0 dtype: float64obj3+obj4 Out[20]: california NaN ohio 7000.0 oregon 32000.0 texas 142000.0 utah NaN dtype: float64 obj4.name='population'obj4.index.name='state'obj4 Out[30]: state california NaN ohio 3500.0 oregon 16000.0 texas 71000.0 Name: population, dtype: float64 obj=Series([4,7,-5,3])obj Out[43]: 0 4 1 7 2 -5 3 3 dtype: int64obj.index=['bob','steve','jeff','ryan']obj Out[45]: bob 4 steve 7 jeff -5 ryan 3 dtype: int64 《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
                            
                        - 上一篇: pandas Series 的索引对象
 - 下一篇: pandas DataFrame 索引