Pandas Series

Pandas Series

A Pandas Series is a one-dimensional labeled array capable of holding data of any type such as integers, floats, strings, or even Python objects. It is similar to a column in a table or a one-dimensional NumPy array, but with labels (index).


Creating a Pandas Series

1. From a 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

2. With Custom Index

s = pd.Series([10, 20, 30], index=["a", "b", "c"])
print(s)

3. From a Dictionary

data = {"Math": 85, "Science": 90, "English": 88}
s = pd.Series(data)
print(s)

(Keys become the index.)


Accessing Series Data

By Index Label

s["Math"]

By Position

s[0]

Multiple Values

s[["Math", "English"]]

Series Attributes

s.index # index labels
s.values # data values
s.dtype # data type
s.size # number of elements

Basic Operations on Series

s + 5 # add 5 to each value
s * 2 # multiply each value
s[s > 85] # filtering

Handling Missing Values

s = pd.Series([10, None, 30])
s.isnull()
s.fillna(0)

Useful Series Methods

s.sum()
s.mean()
s.max()
s.min()
s.sort_values()

Example: Real-World Use

sales = pd.Series([5000, 7000, 6500], index=["Jan", "Feb", "Mar"])
print(sales["Feb"])

Key Points

  • Series is 1-dimensional

  • Has index + values

  • Supports vectorized operations

  • Foundation of Pandas DataFrame


Conclusion

A Pandas Series is a simple yet powerful data structure that makes data handling efficient and intuitive. Understanding Series is essential before moving to DataFrames.

You may also like...