Pandas Series – 1D Labeled Array for Powerful Data Handling
Introduction – What is a Pandas Series?
A Pandas Series is a one-dimensional labeled array capable of holding any data type—integers, floats, strings, objects, or even Python functions. It’s the fundamental building block of Pandas and is often used to store a single column of data in a DataFrame.
In this guide, you’ll learn:
- How to create and manipulate Pandas Series
- Accessing and slicing Series data
- Using custom indexes and performing operations
- Handling missing values and applying functions
1. Creating a Pandas Series
import pandas as pd
data = [10, 20, 30]
series = pd.Series(data)
print(series)
Output:
0 10
1 20
2 30
dtype: int64
Default index is auto-generated from 0 to n-1.
With Custom Index
series = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
print(series)
Output:
a 10
b 20
c 30
dtype: int64
Index can be any list-like object: strings, dates, numbers.
2. Accessing Data in Series
Access by Label or Position
print(series['b']) # Access by label
print(series[1]) # Access by position
Slicing Series
print(series['a':'c']) # Label-based slice (inclusive)
print(series[0:2]) # Position-based slice (exclusive of end)
3. Series Operations
Pandas Series supports vectorized arithmetic and broadcasting:
print(series + 5) # Add 5 to each element
print(series * 2) # Multiply each element by 2
You can also perform element-wise operations between two Series:
s1 = pd.Series([1, 2, 3])
s2 = pd.Series([4, 5, 6])
print(s1 + s2)
4. Handling Missing Data
series = pd.Series([1, None, 3])
print(series.isnull()) # Detect missing
print(series.fillna(0)) # Replace missing with 0
Missing values are represented as NaN
5. Common Series Methods
| Method | Description |
|---|---|
series.head(n) | First n values |
series.tail(n) | Last n values |
series.dtype | Data type of elements |
series.index | Index labels |
series.values | Underlying data as NumPy array |
series.sort_values() | Sort values ascending |
series.unique() | Return unique values |
series.value_counts() | Frequency count of unique values |
6. Applying Functions to Series
You can apply custom or built-in functions to Series data.
Using apply()
print(series.apply(lambda x: x * 2))
Using NumPy functions
import numpy as np
print(np.sqrt(series))
Summary – Recap & Next Steps
Pandas Series offers fast and flexible handling of labeled 1D data. It behaves like a NumPy array but with the added power of labels, missing data support, and function application.
Key Takeaways:
- Series is a 1D labeled array; ideal for single-column or vector-style data
- Supports indexing, slicing, arithmetic, and broadcasting
- Integrates well with NumPy and Python functions
- Useful for exploration, transformation, and analysis
Real-world relevance: Series is often used to handle single columns in datasets, compute stats, clean data, or perform quick transformations during preprocessing.
FAQs – Pandas Series
What is the difference between a Series and a DataFrame?
A Series is 1D, like a single column; a DataFrame is 2D, like a full table.
Can Series hold different data types?
No. A Series must be of a single dtype, although it can be object to mix types.
How can I change the index of a Series?
Use:
series.index = ['x', 'y', 'z']
What if I perform operations between Series with different indexes?
Pandas aligns indexes automatically and fills unmatched values with NaN.
How to convert Series to list or array?
Use .tolist() for list and .values for NumPy array:
series.tolist()
series.values
Share Now :
