Python Pandas SeriesThe Pandas Series can be defined as a one-dimensional array that is capable of storing various data types. We can easily convert the list, tuple, and dictionary into series using "series' method. The row labels of series are called the index. A Series cannot contain multiple columns. It has the following parameter:
Creating a Series:We can create a Series in two ways:
Create an Empty Series:We can easily create an empty series in Pandas which means it will not have any value. The syntax that is used for creating an Empty Series: The below example creates an Empty Series type object that has no values and having default datatype, i.e., float64. Example Output Series([], dtype: float64) Creating a Series using inputs:We can create Series by using various inputs:
Creating Series from Array: Before creating a Series, firstly, we have to import the numpy module and then use array() function in the program. If the data is ndarray, then the passed index must be of the same length. If we do not pass an index, then by default index of range(n) is being passed where n defines the length of an array, i.e., [0,1,2,....range(len(array))-1]. Example Output 0 P 1 a 2 n 3 d 4 a 5 s dtype: object Create a Series from dict We can also create a Series from dict. If the dictionary object is being passed as an input and the index is not specified, then the dictionary keys are taken in a sorted order to construct the index. If index is passed, then values correspond to a particular label in the index will be extracted from the dictionary. Output x 0.0 y 1.0 z 2.0 dtype: float64 Create a Series using Scalar: If we take the scalar values, then the index must be provided. The scalar value will be repeated for matching the length of the index. Output 0 4 1 4 2 4 3 4 dtype: int64 Accessing data from series with Position:Once you create the Series type object, you can access its indexes, data, and even individual elements. The data in the Series can be accessed similar to that in the ndarray. Output 1 Series object attributesThe Series attribute is defined as any information related to the Series object such as size, datatype. etc. Below are some of the attributes that you can use to get the information about the Series object:
Retrieving Index array and data array of a series objectWe can retrieve the index array and data array of an existing Series object by using the attributes index and values. Output RangeIndex(start=0, stop=4, step=1) [2 4 6 8] Index(['a', 'b', 'c'], dtype='object') [11.2 18.6 22.5] Retrieving Types (dtype) and Size of Type (itemsize)You can use attribute dtype with Series object as <objectname> dtype for retrieving the data type of an individual element of a series object, you can use the itemsize attribute to show the number of bytes allocated to each data item. Output int64 8 float64 8 Retrieving ShapeThe shape of the Series object defines total number of elements including missing or empty values(NaN). Output (4,) (3,) Retrieving Dimension, Size and Number of bytes:Output 1 1 4 3 32 24 Checking Emptiness and Presence of NaNsTo check the Series object is empty, you can use the empty attribute. Similarly, to check if a series object contains some NaN values or not, you can use the hasans attribute. Example Output False False True True False False 4 3 3 3 Series FunctionsThere are some functions used in Series which are as follows:
Next TopicPandas Series.map() |