A Pandas Series is a one-dimensional array that can hold data of any type (integers, strings, floats, etc.) and comes with labeled indexes, similar to a column in a spreadsheet or a dictionary.
Creating a Pandas Series
1. Creating a Series from a List
import pandas as pd
data = [10, 20, 30, 40]
series = pd.Series(data)
print(series)
Output:
0 10
1 20
2 30
3 40
dtype: int64
Here, the default index is numeric (0, 1, 2, …).
2. Creating a Series with Custom Index
data = [10, 20, 30, 40]
index_labels = ['a', 'b', 'c', 'd']
series = pd.Series(data, index=index_labels)
print(series)
Output:
a 10
b 20
c 30
d 40
dtype: int64
The index can be customized, making it similar to a dictionary.
3. Creating a Series from a Dictionary
data = {'Alice': 85, 'Bob': 90, 'Charlie': 78}
series = pd.Series(data)
print(series)
Output:
Alice 85
Bob 90
Charlie 78
dtype: int64
When using a dictionary, the keys become the index.
Accessing Elements in a Series
1. Access by Index Position
print(series[0]) # First element
2. Access by Index Label
print(series['Bob']) # 90
3. Slicing a Series
print(series[0:2]) # First two elements
Output:
Alice 85
Bob 90
dtype: int64
Operations on Pandas Series
1. Arithmetic Operations
series = pd.Series([10, 20, 30, 40])
print(series + 5) # Add 5 to each element
Output:
0 15
1 25
2 35
3 45
dtype: int64
2. Applying Functions
print(series.apply(lambda x: x * 2)) # Multiply each element by 2
Checking for Null Values
data = [10, 20, None, 40]
series = pd.Series(data)
print(series.isnull()) # Check for missing values
print(series.notnull()) # Check for non-null values
Summary of Pandas Series
✅ One-dimensional labeled array
✅ Supports various data types
✅ Custom index support
✅ Supports dictionary-like operations
✅ Built-in functions for data manipulation