Table of Contents: Pandas Series
1. Introduction to Series
└── 1.1 What is a Series?
└── 1.2 Key Features of Series
2. Creating a Series
├── 2.1 From Python List
├── 2.2 With Custom Index
├── 2.3 From Dictionary
├── 2.4 From Scalar Value
3. Accessing Elements
├── 3.1 By Position
├── 3.2 By Label
├── 3.3 Slicing (By Position and Label)
4. Operations on Series
├── 4.1 Vectorized Operations
├── 4.2 Conditional Filtering
├── 4.3 Mathematical Functions
5. Attributes of a Series
├── 5.1 `.index`
├── 5.2 `.values`
├── 5.3 `.dtype`
6. Handling Missing Data
├── 6.1 Checking for Missing (`isnull()`)
├── 6.2 Filling Missing (`fillna()`)
7. Series vs Dictionary
├── 7.1 Key Lookup
├── 7.2 Safe Retrieval using `.get()`
8. Use Cases of Series
├── 8.1 Time Series Data
├── 8.2 Sensor Readings
├── 8.3 Mapping Tables
├── 8.4 Intermediate Processing
├── 8.5 Enhanced NumPy Arrays
├── 8.6 Data Preprocessing
9. Advanced Examples
├── 9.1 Series with Datetime Index
├── 9.2 Applying Custom Functions
├── 9.3 Sorting Series
├── 9.4 Combining Series
10. Summary
└── 10.1 Key Takeaways of Pandas Series
A Series
in Pandas is a one-dimensional labeled array that can hold any data type: integers, strings, floats, Python objects, etc. It’s similar to a column in an Excel sheet or SQL table.
🔹 Core Features of Series
- Homogeneous data
- Axis labels (index)
- Size immutable
- Value mutable
- Built on NumPy arrays
✅ 1. Creating a Series
A. From a Python List
import pandas as pd
data = [10, 20, 30, 40]
s = pd.Series(data)
print(s)
Output:
0 10
1 20
2 30
3 40
dtype: int64
Default index is 0,1,2,…
B. With Custom Index
s = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
print(s)
a 10
b 20
c 30
dtype: int64
C. From Dictionary
data = {'a': 100, 'b': 200, 'c': 300}
s = pd.Series(data)
print(s)
a 100
b 200
c 300
dtype: int64
D. From Scalar Value
s = pd.Series(5, index=['a', 'b', 'c'])
print(s)
a 5
b 5
c 5
dtype: int64
✅ 2. Accessing Elements in Series
A. By Position (like lists)
print(s[0]) # Access first element
B. By Label (like dicts)
print(s['a']) # Access by index label
C. Slicing
print(s[0:2]) # By position
print(s['a':'b']) # By label
✅ 3. Operations on Series
A. Vectorized Operations
s = pd.Series([10, 20, 30])
print(s * 2)
0 20
1 40
2 60
dtype: int64
B. Conditional Filtering
print(s[s > 15])
1 20
2 30
dtype: int64
C. Mathematical Functions
print(s.mean())
print(s.sum())
print(s.max())
✅ 4. Attributes of Series
s = pd.Series([1, 2, 3], index=['x', 'y', 'z'])
print(s.index) # Index(['x', 'y', 'z'])
print(s.values) # array([1, 2, 3])
print(s.dtype) # int64
✅ 5. Series with Missing Data
s = pd.Series([1, None, 3])
print(s)
print(s.isnull()) # Check missing
print(s.fillna(0)) # Fill missing
✅ 6. Series vs Dictionary
data = {'a': 1, 'b': 2}
s = pd.Series(data)
print('a' in s) # True
print(s.get('c', 'Not Found')) # Not Found
✅ 7. Use Cases of Series
Use Case | Description |
---|---|
1. Time Series Data | Stock prices, temperature over time |
2. Sensor Readings | IoT devices readings over time |
3. Mapping Tables | Map IDs to values (like country code to names) |
4. Intermediate Processing | When working on a single column of a DataFrame |
5. Indexing/Numpy Enhancement | More functionality than NumPy 1D array |
6. Data Preprocessing | Filling missing values, transformations |
✅ 8. Advanced Series Examples
A. Creating a Series with DateTime Index
import pandas as pd
dates = pd.date_range('2023-01-01', periods=5)
values = [1, 2, 3, 4, 5]
s = pd.Series(values, index=dates)
print(s)
B. Applying Custom Function
s = pd.Series([1, 2, 3, 4])
print(s.apply(lambda x: x ** 2)) # Square each element
C. Sorting
s = pd.Series([10, 5, 8], index=['a', 'b', 'c'])
print(s.sort_values()) # Sort by value
print(s.sort_index()) # Sort by index
D. Combining Series
s1 = pd.Series([1, 2], index=['a', 'b'])
s2 = pd.Series([3, 4], index=['b', 'c'])
print(s1 + s2)
a NaN
b 5.0
c NaN
dtype: float64
🧠Summary
Feature | Series Description |
---|---|
One-dimensional | Single column of data |
Supports indexing | Label-based access |
NumPy-compatible | Works with vectorized operations |
Missing values | Supports NaN |
Ideal for time data | Supports DatetimeIndex |
If you’d like a visual diagram of Series vs DataFrame, or want this in a Notion or PDF format, let me know!