1. Introduction: Transitioning to the Pandas Ecosystem
In data science, machine learning, and artificial intelligence, algorithms require specialized data structures designed for processing, transforming, and analyzing large volumes of structured data.
While NumPy focuses on low-level numerical arrays and mathematical computations, the Pandas library provides high-level data structures that make data manipulation intuitive and robust. Pandas supports arrays, series, and matrices so that data science and machine learning algorithms can operate efficiently. The foundational one-dimensional data structure in Pandas is the Series.
2. What Is a Pandas Series?
A Pandas Series is a one-dimensional (1D) labeled array.
- Index–value pairs: Every value stored in a Series is associated with an identifying label called an index.
- Spreadsheet & database analogy: A Series is like a labeled column in Microsoft Excel or a single attribute column in an SQL table.
- Automatic type inference: When you pass data into a Series, Pandas evaluates the elements and infers their data type (such as 64-bit integers) without manual type declarations.
3. Setting Up the Environment in Jupyter Notebook
Step 1: Installing and Verifying Pandas
To install or verify Pandas in a Jupyter Notebook, run pip with an exclamation mark:
# Installing or checking Pandas inside a Jupyter Notebook cell
!pip install pandas
This installs Pandas (e.g. version 2.2.3) and its dependencies, including NumPy. If Pandas is already present, it confirms the active version.
Step 2: Importing Pandas with the Industry-Standard Alias
The established convention is to import Pandas as pd:
# Industry-standard import alias for Pandas
import pandas as pd
4. Creating a Pandas Series with Custom Labels
Use the pd.Series() constructor. By default, positions are 0, 1, 2…, but you can define custom labels with the index parameter:
import pandas as pd
# Creating a 5-element Series with custom index labels
s = pd.Series([10, 20, 20, 30, 40], index=['a', 'b', 'c', 'd', 'e'])
# Displaying the Series
print(s)
Output:
a 10
b 20
c 20
d 30
e 40
dtype: int64
- Index labels appear on the left; stored values on the right.
- 10 is labeled
'a', 20 labeled 'b' and 'c', and so on.
- Pandas reports the Series as 64-bit integers (
int64).
5. Essential Series Attributes: .values, .index, and .dtype
Dedicated attributes unpack the components of a Series:
# 1. Extracting only the underlying numerical values
print("Values: ", s.values)
# 2. Extracting only the index labels
print("Index: ", s.index)
# 3. Inspecting the data type
print("Data Type:", s.dtype)
Example output:
Values: [10 20 20 30 40]
Index: Index(['a', 'b', 'c', 'd', 'e'], dtype='object')
Data Type: int64
s.values — the stored numbers without labels
s.index — the labels ('a' through 'e'), as an Index object
s.dtype — internal type of the values (int64)
6. Common Series Inspection Methods
A. Inspecting Initial and Final Rows: .head() and .tail()
On large datasets, printing everything is impractical. Use .head() and .tail() to sample quickly:
# Inspecting the beginning rows of the Series
print("Head:")
print(s.head())
# Inspecting the concluding rows of the Series
print("\nTail:")
print(s.tail())
s.head() shows the first rows.
s.tail() shows the last rows.
- On a five-row sample Series, both methods show all rows; on large data they give a fast structure check.
B. Frequency Distribution: .value_counts()
To count how often each unique value appears, use .value_counts():
# Counting occurrences of each unique value
counts = s.value_counts()
print("Value Counts:")
print(counts)
Output:
20 2
10 1
30 1
40 1
Name: count, dtype: int64
The method counts each unique value and sorts by frequency: 20 appears twice; 10, 30, and 40 once each. It is a core tool in exploratory data analysis (EDA) for spotting patterns.