Today in this article we are going to learn about the Pandas Series Attributes. The series object is similar to the data frame object that we have in the Pandas library. The series object has many attributes that help us to determine the different data associated with the series. In this, we have the size, dimensions, values, etc.
So we will learn about all these series attributes and we will see examples of each attribute to understand how they work. Let us begin with our tutorial:
Example : Create a series object
We will start by creating a Pandas series object and then we will see the different attributes that this object has built-in.
import pandas as pd
import numpy as np
ser = pd.Series([1, 2, 3, 4, 5, np.nan])
print("The Series object is : ",ser)
Output
The Series object is : 0 1.0
1 2.0
2 3.0
3 4.0
4 5.0
5 NaN
dtype: float64
Series.index
The series. index (axis labels) attribute of the Series gives us the information about the start index and end index of the series object. It also tells us about the step size of this series.
import pandas as pd
import numpy as np
ser = pd.Series([1, 2, 3, 4, 5, np.nan])
print("The Series index is : ",ser.index)
Output
The Series index is : RangeIndex(start=0, stop=6, step=1)
Series.array
The Series. array attribute gives us the Extension Array of the data backing this Series or Index. Along with the array it also gives us the details about the length and data type of the elements it holds.
import pandas as pd
import numpy as np
ser = pd.Series([1, 2, 3, 4, 5, np.nan])
print("The Series array is : ",ser.array)
Output
The Series array is : <PandasArray>
[1.0, 2.0, 3.0, 4.0, 5.0, nan]
Length: 6, dtype: float64
Series.values
Return Series as ndarray or ndarray-like depending on the dtype.
import pandas as pd
import numpy as np
ser = pd.Series([1, 2, 3, 4, 5, np.nan])
print("The Series values are : ",ser.values)
Output
The Series values are : [ 1. 2. 3. 4. 5. nan]
Series.dtype
Return the dtype object of the underlying data.
import pandas as pd
import numpy as np
ser = pd.Series([1, 2, 3, 4, 5, np.nan])
print("The Series dtype is : ",ser.dtype)
Output
The Series dtype is : float64
Series.shape
Return a tuple of the shape of the underlying data.
import pandas as pd
import numpy as np
ser = pd.Series([1, 2, 3, 4, 5, np.nan])
print("The Series shape is : ",ser.shape)
Output
The Series shape is : (6,)
Series.nbytes
Return the number of bytes in the underlying data.
import pandas as pd
import numpy as np
ser = pd.Series([1, 2, 3, 4, 5, np.nan])
print("The Series nbytes is : ",ser.nbytes)
Output
The Series nbytes is : 48
Series.ndim
The number of dimensions of the underlying data, by definition 1.
import pandas as pd
import numpy as np
ser = pd.Series([1, 2, 3, 4, 5, np.nan])
print("The Series ndim is : ",ser.ndim)
Output
The Series ndim is : 1
Series.size
Return the number of elements in the underlying data.
import pandas as pd
import numpy as np
ser = pd.Series([1, 2, 3, 4, 5, np.nan])
print("The Series size is : ",ser.size)
Output
The Series size is : 6
Series.T
Return the transpose, which is by definition self.
import pandas as pd
import numpy as np
ser = pd.Series([1, 2, 3, 4, 5, np.nan])
print("The Series Transpose is : ",ser.T)
Output
The Series Transpose is : 0 1.0
1 2.0
2 3.0
3 4.0
4 5.0
5 NaN
dtype: float64
Series.hasnans
Return the memory usage of the Series.Series.memory_usage([index, deep]).Return if I have any nans; enables various perf speedups.
import pandas as pd
import numpy as np
ser = pd.Series([1, 2, 3, 4, 5, np.nan])
print("The Series has nans : ",ser.hasnans)
Output
The Series has nans : True
Series.empty
Indicator whether DataFrame is empty.
import pandas as pd
import numpy as np
ser = pd.Series([1, 2, 3, 4, 5, np.nan])
print("The Series is empty: ",ser.empty)
Output
The Series is empty: False
Series.dtypes
Return the dtype object of the underlying data.
import pandas as pd
import numpy as np
ser = pd.Series([1, 2, 3, 4, 5, np.nan])
#Indicator whether DataFrame is empty.
print("The Series dtypes are : ",ser.dtypes)
Output
The Series dtypes are : float64
Series.name
Return the name of the Series.
import pandas as pd
import numpy as np
s = pd.Series([1, 2, 3], name='MySeries')
print("The Series name is : ",s.name)
Output
The Series name is : MySeries