Series attributes
Once you have a pd.Series
, there are quite a few attributes you may want to inspect. The most basic attributes can tell you the type and size of your data, which is often the first thing you will inspect when reading in data from a data source.
How to do it
Let’s start by creating a pd.Series
that has a name, alongside a custom pd.Index
, which itself has a name. Although not all of these elements are required, having them will help us more clearly understand what the attributes we access through this recipe are actually showing us:
index = pd.Index(["dog", "cat", "human"], name="animal")
ser = pd.Series([4, 4, 2], name="num_legs", index=index)
ser
animal
dog 4
cat 4
human 2
Name: num_legs, dtype: int64
The first thing users typically want to know about their data is the type of pd.Series
. This can be inspected via the pd.Series.dtype
attribute:
ser.dtype
dtype('int64')
The name may be inspected via the pd.Series.name
attribute. The data we constructed in this recipe was created with the name="num_legs"
argument, which is what you will see when accessing this attribute (if not provided, this will return None
):
ser.name
num_legs
The associated pd.Index
can be accessed via pd.Series.index
:
ser.index
Index(['dog', 'cat', 'human'], dtype='object', name='animal')
The name of the associated pd.Index
can be accessed via pd.Series.index.name
:
ser.index.name
animal
The shape can be accessed via pd.Series.shape
. For a one-dimensional pd.Series
, the shape is returned as a one-tuple where the first element represents the number of rows:
ser.shape
3
The size (number of elements) can be accessed via pd.Series.size
:
ser.size
3
The Python built-in function len
can show you the length (number of rows):
len(ser)
3