What Is a Pandas Series?
A Pandas Series is a one-dimensional labeled array capable of holding data of any type such as integers, floats, strings, or Python objects. Each value in a Series is associated with an index, which allows for fast and intuitive data access.
You can think of a Series as a single column of a table with row labels.
Why Use Pandas Series?
A Pandas Series is useful because it:
-
Stores labeled data
-
Supports fast indexing and slicing
-
Handles missing values (NaN) gracefully
-
Performs vectorized operations
-
Integrates seamlessly with NumPy
Creating a Pandas Series
1. Create Series from a List
import pandas as pd s = pd.Series([10, 20, 30, 40]) print(s) # Output: # 0 10 # 1 20 # 2 30 # 3 40 # dtype: int64If no index is provided, Pandas assigns a default integer index starting from 0.
2. Create Series with Custom Index
import pandas as pds = pd.Series([100, 200, 300], index=["a", "b", "c"]) print(s) # Output: # a 100 # b 200 # c 300 # dtype: int64Custom indices make data more meaningful and readable.
3. Create Series from a Dictionary
import pandas as pd data = {"Math": 85, "Science": 90, "English": 88} s = pd.Series(data) print(s) # Output: # Math 85 # Science 90 # English 88 # dtype: int64Dictionary keys become index labels, and values become Series values.
4. Create Series with a Scalar Value
import pandas as pd s = pd.Series(5, index=["a", "b", "c"]) print(s) # Output: # a 5 # b 5 # c 5 # dtype: int64The scalar value is repeated for each index label.
Accessing Elements in a Series
Access by Index Position
import pandas as pd s = pd.Series([10, 20, 30, 40]) print(s[0]) print(s[2]) # Output: # 10 # 30Access by Index Label
import pandas as pd s = pd.Series([100, 200, 300], index=["a", "b", "c"]) print(s["b"]) # Output: # 200Series Operations
Pandas Series supports vectorized operations, meaning operations are applied element-wise.
import pandas as pd s = pd.Series([1, 2, 3, 4]) print(s + 10) # Output: # 0 11 # 1 12 # 2 13 # 3 14 # dtype: int64Handling Missing Values
Pandas automatically handles missing data using NaN.
import pandas as pd s = pd.Series([10, 20, None, 40]) print(s) # Output: # 0 10.0 # 1 20.0 # 2 NaN # 3 40.0 # dtype: float64Useful Series Attributes
| Attribute | Description |
|---|---|
s.index | Returns index labels |
s.values | Returns underlying NumPy array |
s.dtype | Returns data type |
s.size | Number of elements |
s.name | Name of the Series |
Difference Between Pandas Series and NumPy Array
| Feature | Pandas Series | NumPy Array |
|---|---|---|
| Dimension | 1D | N-dimensional |
| Labels | Yes | No |
| Missing Values | Supported | Limited |
| Data Type | Can be mixed | Homogeneous |
Key Points to Remember
-
A Series is a one-dimensional labeled array
-
Index labels make data access intuitive
-
Supports vectorized operations
-
Handles missing values efficiently
-
Forms the foundation of Pandas DataFrames