Pandas - Series Attributes
Series attributes reflect information that is intrinsic to the series. Accessing a series through its attributes allows us to get the intrinsic properties of the series. Most commonly used attributes are mentioned below:
Function | Description |
---|---|
Series.dtype | Return the dtype object of the underlying data. |
Series.empty | Indicates whether DataFrame is empty. |
Series.index | Returns the index (axis labels) of the Series. |
Series.ndim | Number of dimensions of the underlying data, by definition 1. |
Series.shape | Return a tuple of the shape of the underlying data. |
Series.size | Number of elements in the underlying data. |
Series.values | Return the actual data in the series as an array. |
Lets discuss these attributes in detail:
Series.dtype
The dtype attribute is used to get the dtype object of the given series. Consider the following example.
import pandas as pd x = pd.Series([10, 20, 30]) y = pd.Series(["abc", "xyz"]) print("dtype of x:", x.dtype) print("dtype of y:", y.dtype)
The output of the above code will be:
dtype of x: int64 dtype of y: object
Series.empty
The empty attribute is used to check whether the given Series is empty or not.
import pandas as pd Name = ['John', 'Marry', 'Jo', 'Sam'] x = pd.Series(Name) y = pd.Series(dtype="float64") print("Is x empty?:", x.empty) print("Is y empty?:", y.empty)
The output of the above code will be:
Is x empty?: False Is y empty?: True
Series.index
The index attribute is used to return the index (axis labels) of the Series.
import pandas as pd Name = ['John', 'Marry', 'Jo', 'Sam'] x = pd.Series(Name) print("The Series contains:") print(x) print("\nThe index (axis labels) are:") print(x.index)
The output of the above code will be:
The Series contains: 0 John 1 Marry 2 Jo 3 Sam dtype: object The index (axis labels) are: RangeIndex(start=0, stop=4, step=1)
The above result is written in a compact format which can be interpreted as [0, 1, 2, 3].
Series.ndim
The ndim attribute is used to get the dimensions of the given Series, which is by definition should be 1.
import pandas as pd Colors = ['Red', 'Blue', 'Green', 'White'] x = pd.Series(Colors) #dimension of x print("Dimension of x:", x.ndim)
The output of the above code will be:
Dimension of x: 1
Series.shape
The shape attribute can be used to get a tuple of the shape of the underlying data. Consider the following example.
import pandas as pd Numbers = [60, 55, 62, 58, 78] y = pd.Series(Numbers) #shape of y print("Shape of y:", y.shape)
The output of the above code will be:
Shape of y: (5,)
Series.size
The size attribute is used to get number of elements in the given Series. Consider the example below:
import pandas as pd Name = ['John', 'Marry', 'Jo', 'Sam'] x = pd.Series(Name) print("The Series is:") print(x) print("\nThe number of elements in the Series:", x.size)
The output of the above code will be:
The Series is: 0 John 1 Marry 2 Jo 3 Sam dtype: object The number of elements in the Series: 4
Series.values
The values attribute is used to return the actual data in the series as an array. Consider the following example:
import pandas as pd Name = ['John', 'Marry', 'Jo', 'Sam'] x = pd.Series(Name) print("The Series is:") print(x) print("\nThe actual data in the series is:") print(x.values)
The output of the above code will be:
The Series is: 0 John 1 Marry 2 Jo 3 Sam dtype: object The actual data in the series is: ['John' 'Marry' 'Jo' 'Sam']