This article describes the series data type in pandas in detail, shared with you as follows:
import pandas as pd import numpy as np import names ''' Write in front of the words: 1, series and array type of difference for the series has an index, while the other does not; series in the data must be one-dimensional, while the array type is not necessarily 2, you can see the series as a fixed-length ordered dictionary, you can get the properties of the series through the shape, index, values and so on. ''' # 1. series creation ''' (1) Created from a list or numpy array The default index is an integer-type index from 0 to N-1, e.g. s1; the The index can be specified by setting the index parameter, such as s2; created in this way series, not a copy of the array, that is, the operation of the series at the same time also changed the original array array, such as s3 (2) created by the dictionary The key of the dictionary is called the index, the key value is the value, such as s4; ''' n1 = ([1, 4, 5, 67, 7, 43, ]) s1 = (n1) # print(s1) ''' 1 4 5 67 7 43 dtype: int32 ''' s2 = (n1, index=['a', 'b', 'c', 'd', 'e', 'f']) # print(s2) ''' a 1 b 4 c 5 d 67 e 7 f 43 dtype: int32 ''' # print(n1) ''' [ 1 4 5 67 7 43] ''' s1[2] = 100 s3 = s1 # print(s3) ''' 1 4 100 67 7 43 dtype: int32 ''' # print(n1) ''' [ 1 4 100 67 7 43] ''' dict1 = {} for i in range(10, 15): # names.get_last_name(), randomly generate English names dict1[names.get_last_name()] = i s4 = (dict1) # print(s4) ''' Poole 10 Allen 11 Davis 12 Roland 13 Brehm 14 dtype: int64 ''' # 2. indexing of series ''' (1) through the index of the value, you can get through the subscript, you can also specify the index to get, such as s6, s7 (2) fetch by .loc[] (display index), this way can only fetch the displayed index, can not be obtained by subscript, such as s7 (recommended) (3) implicit index, use integer as index value, use .icol[], such as s9 (recommended) ''' s5 = (([1, 5, 9, 7, 6, 4, 52, 8]), index=[list('abcdefgh')]) # print(s5) ''' a 1 b 5 c 9 d 7 e 6 f 4 g 52 h 8 dtype: int32 ''' s6 = s5[2] # print(s6) ''' ''' s7 = s5['c'] # print(s7) ''' c 9 dtype: int32 ''' s8 = ['c'] # print(s8) ''' c 9 dtype: int32 ''' s9 = [2] # print(s9) ''' ''' # 3, slicing of series ''' 1, series slicing and list usage is similar, the difference is that it is recommended to use .loc[:] and .iloc[:], such as s10 and s11, of course, direct use of [:] can also be used. 2, when encountered a particularly long series, we support the removal of the first 5 or the last 5 data can be directly used .head () or .tail () ''' s5 = (([1, 5, 9, 7, 6, 4, 52, 8]), index=[list('abcdefgh')]) # print(s5) ''' a 1 b 5 c 9 d 7 e 6 f 4 g 52 h 8 dtype: int32 ''' s10 = ['b':'g'] # print(s10) ''' b 5 c 9 d 7 e 6 f 4 g 52 dtype: int32 ''' s11 = [1:7] # print(s11) ''' b 5 c 9 d 7 e 6 f 4 g 52 dtype: int32 ''' # 4, on NaN ''' (1) NaN stands for null, but is not the same as None, which has a different data type, <class '.NoneType' > and NaN is of type <class 'float'>; (2)It is possible to use(),(),or bring your ownisnull(),notnull()Function detects missing data ''' # print(type(None),type()) ''' <class 'NoneType'> <class 'float'> ''' s12 = ([1,2,None,],index=list('Beacon Thunder and Lightning')) # print(s12) ''' Beacon 1.0 Fire 2.0 Thunder NaN Electricity NaN dtype: float64 ''' # print((s12)) ''' Beacon False Fire False Thunder True Electricity True dtype: bool ''' # print((s12)) ''' Beacon True Fire True Thunder False Electricity False dtype: bool ''' # print(()) ''' Beacon True Fire True Thunder False Electricity False dtype: bool ''' # print(()) ''' Beacon False Fire False Thunder True Electricity True dtype: bool ''' # Fetch the value in the series that is not null # print(s12[()]) ''' Beacon 1.0 Fire 2.0 dtype: float64 ''' # The name attribute of a series ''' ''' = 'Feng Shui' # print(s12) ''' Beacon 1.0 Fire 2.0 Thunder NaN Electricity NaN Name: feng shui, dtype: float64 '''
This is the whole content of this article.