YouTip LogoYouTip

Pandas Series

Series is a core data structure in Pandas, similar to a one-dimensional array, with data and indexes. Series can store any data type (integers, floats, strings, etc.) and access elements through labels (indexes). The Series data structure is very useful because it can handle various data types while maintaining efficient data manipulation capabilities, such as quick access and operation of data through labels. ### Series Features: * **One-dimensional array:** Each element in Series has a corresponding index value. * **Index:** Each data element can be accessed through labels (indexes). By default, the index starts from 0, but you can also customize the index. * **Data type:** `Series` can hold elements of different data types, including integers, floats, strings, Python objects, etc. * **Immutability of size:** The size of Series remains unchanged after creation, but it can be changed through certain operations (such as append or delete). * **Operations:** Series supports various operations, such as mathematical operations, statistical analysis, string processing, etc. * **Missing data:** Series can contain missing data, and Pandas uses NaN (Not a Number) to represent missing or null values. * **Automatic alignment:** When performing operations on multiple Series, Pandas automatically aligns data based on indexes, making data processing more efficient. We can use the Pandas library to create a Series object and specify its Index, Name, and Values: !(#) ## Example import pandas as pd # Create a Series object with name 'A' and values 1, 2, 3, 4 # Default index is 0, 1, 2, 3 series = pd.Series([1,2,3,4], name='A') # Display the Series object print(series) # If you want to explicitly set the index, you can do this: custom_index =[1,2,3,4]# Custom index series_with_index = pd.Series([1,2,3,4], index=custom_index, name='A') # Display the Series object with custom index print(series_with_index) The output is: 0 11 22 33 4Name: A, dtype: int64 1 12 23 34 4Name: A, dtype: int64 Series is a basic data structure in Pandas, similar to a one-dimensional array or list, but with labels (indexes), making data more flexible for processing and analysis. The following is a detailed introduction to Series in Pandas. * * * ## Create Series You can use the pd.Series() constructor to create a Series object, passing in a data array (which can be a list, NumPy array, etc.) and an optional index array. pandas.Series(data=None, index=None, dtype=None, name=None, copy=False, fastpath=False) Parameter description: * `data`: The data part of Series, which can be a list, array, dictionary, scalar value, etc. If this parameter is not provided, an empty Series is created. * `index`: The index part of Series, used to label the data. Can be a list, array, index object, etc. If this parameter is not provided, a default integer index is created. * `dtype`: Specifies the data type of Series. Can be NumPy data types, such as `np.int64`, `np.float64`, etc. If this parameter is not provided, the data type is automatically inferred. * `name`: The name of Series, used to identify the Series object. If this parameter is provided, the created Series object will have the specified name. * `copy`: Whether to copy data. Default is False, meaning not to copy data. If set to True, the input data will be copied. * `fastpath`: Whether to enable fast path. Default is False. Enabling fast path may improve performance in some cases. Create a simple Series example: ## Example import pandas as pd a =[1,2,3] myvar = pd.Series(a) print(myvar) The output is as follows: !(#) As can be seen from the figure above, if the index is not specified, the index values start from 0, and we can read data based on the index values: ## Example import pandas as pd a =[1,2,3] myvar = pd.Series(a) print(myvar) The output is as follows: 2 We can also specify index values, as shown in the following example: ## Example import pandas as pd a =["Google","Tutorial","Wiki"] myvar = pd.Series(a, index =["x","y","z"]) print(myvar) The output is as follows: !(#) Read data based on index values: ## Example import pandas as pd a =["Google","Tutorial","Wiki"] myvar = pd.Series(a, index =["x","y","z"]) print(myvar) The output is as follows: Tutorial We can also use key/value objects, similar to dictionaries, to create Series: ## Example import pandas as pd sites ={1: "Google",2: "Tutorial",3: "Wiki"} myvar = pd.Series(sites) print(myvar) The output is as follows: !(#) As can be seen from the figure above, the keys of the dictionary become the index values. If we only need part of the data from the dictionary, we just need to specify the index of the data we need, as shown in the following example: ## Example import pandas as pd sites ={1: "Google",2: "Tutorial",3: "Wiki"} myvar = pd.Series(sites, index =[1,2]) print(myvar) The output is as follows: !(#) Set Series name parameter: ## Example import pandas as pd sites ={1: "Google",2: "Tutorial",3: "Wiki"} myvar = pd.Series(sites, index =[1,2], name="TUTORIAL-Series-TEST") print(myvar) ![Image
← Python UrllibPandas Tutorial β†’