C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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: <series object> = pandas.Series() The below example creates an Empty Series type object that has no values and having default datatype, i.e., float64. Example import pandas as pd x = pd.Series() print (x) 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 import pandas as pd import numpy as np info = np.array(['P','a','n','d','a','s']) a = pd.Series(info) print(a) 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. #import the pandas library import pandas as pd import numpy as np info = {'x' : 0., 'y' : 1., 'z' : 2.} a = pd.Series(info) print (a) 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. #import pandas library import pandas as pd import numpy as np x = pd.Series(4, index=[0, 1, 2, 3]) print (x) 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. import pandas as pd x = pd.Series([1,2,3],index = ['a','b','c']) #retrieve the first element print (x[0]) 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. import numpy as np import pandas as pd x=pd.Series(data=[2,4,6,8]) y=pd.Series(data=[11.2,18.6,22.5], index=['a','b','c']) print(x.index) print(x.values) print(y.index) print(y.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. import numpy as np import pandas as pd a=pd.Series(data=[1,2,3,4]) b=pd.Series(data=[4.9,8.2,5.6], index=['x','y','z']) print(a.dtype) print(a.itemsize) print(b.dtype) print(b.itemsize) Output int64 8 float64 8 Retrieving ShapeThe shape of the Series object defines total number of elements including missing or empty values(NaN). import numpy as np import pandas as pd a=pd.Series(data=[1,2,3,4]) b=pd.Series(data=[4.9,8.2,5.6],index=['x','y','z']) print(a.shape) print(b.shape) Output (4,) (3,) Retrieving Dimension, Size and Number of bytes:import numpy as np import pandas as pd a=pd.Series(data=[1,2,3,4]) b=pd.Series(data=[4.9,8.2,5.6], index=['x','y','z']) print(a.ndim, b.ndim) print(a.size, b.size) print(a.nbytes, b.nbytes) 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 import numpy as np import pandas as pd a=pd.Series(data=[1,2,3,np.NaN]) b=pd.Series(data=[4.9,8.2,5.6],index=['x','y','z']) c=pd.Series() print(a.empty,b.empty,c.empty) print(a.hasnans,b.hasnans,c.hasnans) print(len(a),len(b)) print(a.count( ),b.count( )) 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()
|