Informatics Practices · Ch 2 — Data Handling using Pandas – I
Attributes of Series
Attributes of Series
A Series object in Pandas is more than just a labelled array — it carries several built-in properties, called attributes, that let you inspect or label the data directly. You access an attribute by writing the Series name, a dot, and the attribute name (e.g., seriesName.attribute). The textbook uses the Series seriesCapCntry (which maps countries to their capitals) to demonstrate each one.
The name attribute
Every Series can have a name — a label for the entire data column. Initially, a Series has no name. You assign one using series.name = 'some string'. Once set, the name appears in the output when you print the Series, right after the data, as Name: Capitals, dtype: object.
seriesCapCntry.name = 'Capitals'
print(seriesCapCntry)
Output:
India NewDelhi
USA WashingtonDC
UK London
France Paris
Name: Capitals, dtype: object
The index.name attribute
The index itself can also be given a name. This is separate from the Series name. You set it with series.index.name = 'some string'. When printed, the index name appears as a header above the index labels.
seriesCapCntry.index.name = 'Countries'
print(seriesCapCntry)
Output:
Countries
India NewDelhi
USA WashingtonDC
UK London
France Paris
Name: Capitals, dtype: object
Notice that Countries now sits above the index column, while Capitals remains the name of the data column.
The values attribute
values returns a NumPy array containing only the data values — the index labels are stripped away. This is useful when you need to work with the raw data without labels.
print(seriesCapCntry.values)
Output:
['NewDelhi' 'WashingtonDC' 'London' 'Paris']
The result is an array of strings (dtype object), not a Series.
The size attribute
size gives the total number of elements in the Series, including any missing values (NaN). It is an integer.
print(seriesCapCntry.size)
Output:
4
The empty attribute
empty returns a Boolean — True if the Series has zero elements, False otherwise. This is a quick check before performing operations that require data.
print(seriesCapCntry.empty) # False, because it has 4 elements
# Create an empty Series
seriesEmpt = pd.Series()
print(seriesEmpt.empty) # True
An empty Series is one with no data at all — not even a single NaN. A Series containing only NaN values is not empty; its size would be greater than zero.
Activity 2.3 (from the textbook)
The textbook includes a small activity to test your understanding. Given this code:
import pandas as pd
import numpy as np …
| Attribute Name | Purpose | Example |
|---|---|---|
| name | assigns a name to the Series | >>> seriesCapCntry.name = 'Capitals' >>> print(seriesCapCntry) India NewDelhi USA WashingtonDC UK London France Paris Name: Capitals, dtype: object |
| index.name | assigns a name to the index of the series | >>>seriesCapCntry.index.name = 'Countries' >>> print(seriesCapCntry) Countries India NewDelhi USA WashingtonDC UK London France Paris Name: Capitals, dtype: object |
| values | prints a list of the values in the series | >>> print(seriesCapCntry.values) ['NewDelhi' 'WashingtonDC' 'London' 'Paris'] |
| size | prints the number of values in the Series object | >>> print(seriesCapCntry.size) 4 |