PYTHON FOR AI • LESSON 3

Series

A Pandas Series is a one-dimensional labeled data structure. It is similar to a single column in a table, where every value has an index that helps us identify and access it.

CORE IDEA

A Series is like one labeled column of data.

If a DataFrame is a complete table, a Series can be thought of as one column from that table. Each value has a position called an index.

01

What Is a Pandas Series?

A Pandas Series stores a sequence of values in one dimension.

For example, imagine these student scores:

80
90
75
85

We can store these values in a Pandas Series.

import pandas as pd

scores = pd.Series([80, 90, 75, 85])

print(scores)
0    80
1    90
2    75
3    85
dtype: int64

Notice the numbers on the left: 0, 1, 2, 3.

These are the Series indexes.

02

Understanding the Structure

A Series has two important parts:

  • Values — the actual data.
  • Index — labels used to identify the data.

Example

0    80
1    90
2    75
3    85

Here:

  • 80, 90, 75, 85 are the values.
  • 0, 1, 2, 3 are the indexes.
03

Creating a Series With Custom Labels

The default indexes are numbers, but we can provide our own labels.

import pandas as pd

scores = pd.Series(
    [80, 90, 75],
    index=["Raj", "John", "Sarah"]
)

print(scores)
Raj      80
John     90
Sarah    75
dtype: int64

Now the names are the indexes.

This makes the data easier to understand because we can identify the score using the student's name.

04

Access a Series Value

We can access a value using its index.

print(scores["Raj"])
80

We can also access the second value using its position.

print(scores.iloc[1])
90

iloc means that we are accessing data by its integer position.

05

Index vs Position

This distinction is important.

Using the label

print(scores["John"])
90

We used the index label "John".

Using the position

print(scores.iloc[1])
90

We used position 1, meaning the second item.

06

Create a Series From a Dictionary

A dictionary is a natural way to create a labeled Series because dictionary keys can become the indexes.

import pandas as pd

scores = {
    "Raj": 80,
    "John": 90,
    "Sarah": 75
}

series = pd.Series(scores)

print(series)
Raj      80
John     90
Sarah    75
dtype: int64

The dictionary keys became the Series indexes and the dictionary values became the Series values.

07

Perform Calculations on a Series

Pandas allows us to perform mathematical operations on the entire Series.

scores = pd.Series([80, 90, 75, 85])

print(scores.mean())
82.5

We can also find the highest and lowest values.

print(scores.max())
print(scores.min())
90
75

Other useful operations include:

scores.sum()
scores.count()
scores.std()
08

Filter a Series

We can select only values that satisfy a condition.

scores = pd.Series([80, 90, 75, 85])

high_scores = scores[scores >= 85]

print(high_scores)
1    90
3    85
dtype: int64

The condition:

scores >= 85

checks every value and keeps only the values that are greater than or equal to 85.

09

Modify Values

We can perform operations on every value in a Series.

scores = pd.Series([80, 90, 75])

updated_scores = scores + 5

print(updated_scores)
0    85
1    95
2    80
dtype: int64

Five was added to every value automatically.

This is similar to the array operations you learned in NumPy.

10

Series vs DataFrame

This is one of the most important things to understand before moving forward.

Series

scores = pd.Series([80, 90, 75])

A Series is one-dimensional. Think of it as one column.

DataFrame

students = pd.DataFrame({
    "Name": ["Raj", "John", "Sarah"],
    "Score": [80, 90, 75]
})

A DataFrame is two-dimensional. Think of it as a complete table containing multiple columns.

11

Real-World Example

Imagine an AI application that stores the prices of products.

import pandas as pd

prices = pd.Series(
    [999, 499, 1499, 799],
    index=["Laptop", "Mouse", "Monitor", "Keyboard"]
)

print(prices)
Laptop      999
Mouse       499
Monitor    1499
Keyboard    799
dtype: int64

Now we can easily find the most expensive product.

print(prices.max())
1499

We can also retrieve the price of a specific product.

print(prices["Laptop"])
999
12

Why Series Is Important for AI

Machine Learning datasets contain many individual columns such as:

Age
Salary
Experience
PurchaseAmount
Rating

Each column in a Pandas DataFrame is generally represented as a Series.

That means understanding Series is necessary before you start working seriously with DataFrames.

KEY TAKEAWAY

A Pandas Series is one labeled dimension of data.

A Series contains values and an index. The index can be a simple number such as 0, 1, 2, or meaningful labels such as product names or student names. You can access values, filter them, calculate statistics, and perform operations on the entire Series. A DataFrame is built from multiple Series, which is why learning Series first makes DataFrames much easier to understand.