Pandas Series
Learn how to create a Series, assign custom indexes, access values, perform calculations, and work with labelled one-dimensional data.
1. Learning Objectives
After completing this lesson, you will be able to:
- Explain what a Pandas Series is.
- Create a Series from lists and dictionaries.
- Use default and custom indexes.
- Access Series values using labels and positions.
- Inspect values, indexes, and data types.
- Perform calculations and basic filtering.
2. What Is a Pandas Series?
A Series is a one-dimensional labelled data structure provided by Pandas. It contains a sequence of values and an associated index used to identify each value.
A Series is useful when working with a single set of related values, such as student marks, product prices, employee salaries, or daily temperatures.
Figure 1: A Series pairs index labels with values.
3. Creating a Series from a List
The pd.Series() function converts a supported
sequence of values into a Series.
import pandas as pd marks = pd.Series([80, 92, 86]) print(marks)
Output:
0 80 1 92 2 86 dtype: int64
Understanding the Output
0, 1, 2are the default index labels.80, 92, 86are the stored values.dtype: int64indicates the integer data type on this example's environment.
4. Creating a Series with Custom Indexes
You can assign meaningful labels by passing an
index argument. This makes it easier to
access a value using a label rather than a default number.
import pandas as pd
marks = pd.Series(
[80, 92, 86],
index=["Samadhan", "Pradnya", "Samiksha"]
)
print(marks)
Output:
Samadhan 80 Pradnya 92 Samiksha 86 dtype: int64
Access a Value Using Its Label
print(marks["Pradnya"])
Output:
92
5. Creating a Series from a Dictionary
A Python dictionary can also be used to create a Series. Its keys become the index labels and its values become the Series data.
import pandas as pd
student_marks = {
"Samadhan": 80,
"Pradnya": 92,
"Samiksha": 86
}
marks = pd.Series(student_marks)
print(marks)
Output:
Samadhan 80 Pradnya 92 Samiksha 86 dtype: int64
This is convenient when your data already consists of meaningful keys and corresponding values.
6. Accessing Series Values
You can access values using index labels or positional selection. These approaches serve different purposes.
Label-Based Selection
print(marks.loc["Samadhan"]) print(marks.loc["Samiksha"])
Output:
80 86
Position-Based Selection
print(marks.iloc[0]) print(marks.iloc[1])
Output:
80 92
| Method | Purpose | Example |
|---|---|---|
loc |
Select by index label. | marks.loc["Pradnya"] |
iloc |
Select by integer position. | marks.iloc[1] |
loc uses labels, while iloc
uses zero-based integer positions.
7. Inspecting a Series
A Series has attributes and methods that help you inspect its values, index, data type, and summary statistics.
print("Values:", marks.values)
print("Index:", marks.index)
print("Data type:", marks.dtype)
print("Dimensions:", marks.ndim)
print("Size:", marks.size)
print("Shape:", marks.shape)
Example output:
Values: [80 92 86] Index: Index(['Samadhan', 'Pradnya', 'Samiksha'], dtype='object') Data type: int64 Dimensions: 1 Size: 3 Shape: (3,)
The exact representation of an index and data type can vary with the Pandas version and the values used.
8. Calculations with a Series
Numerical Series support common calculations such as total, average, minimum, and maximum.
print("Total:", marks.sum())
print("Average:", marks.mean())
print("Highest:", marks.max())
print("Lowest:", marks.min())
Output:
Total: 258 Average: 86.0 Highest: 92 Lowest: 80
| Method | Meaning |
|---|---|
sum() |
Adds the values. |
mean() |
Calculates the average. |
max() |
Returns the maximum value. |
min() |
Returns the minimum value. |
9. Filtering a Series
Boolean conditions let you select values that satisfy a requirement. For example, you can find students whose marks are greater than or equal to 85.
high_marks = marks[marks >= 85] print(high_marks)
Output:
Pradnya 92 Samiksha 86 dtype: int64
The condition creates a Boolean mask. Pandas uses that mask to keep only the entries whose condition is true.
10. Handling Missing Values
A Series can contain missing values. Pandas provides methods to identify and handle them.
import pandas as pd
marks = pd.Series({
"Samadhan": 80,
"Pradnya": None,
"Samiksha": 86
})
print(marks.isna())
Example output:
Samadhan False Pradnya True Samiksha False dtype: bool
Fill Missing Values
filled_marks = marks.fillna(0) print(filled_marks)
fillna(0) creates a Series where missing
values are filled with zero. Choose a replacement that
makes sense for your data; zero may not always be appropriate.
11. Practical Exercise
CIIT Training Institute: Marks Analysis
Create a Series containing the marks of Samadhan, Pradnya, and Samiksha.
Complete these tasks:
- Assign student names as index labels.
- Print Pradnya's marks.
- Display the average marks.
- Find the highest and lowest marks.
- Filter students with marks of 85 or above.
Practice Code
import pandas as pd
marks = pd.Series(
[80, 92, 86],
index=["Samadhan", "Pradnya", "Samiksha"]
)
print("Pradnya:", marks.loc["Pradnya"])
print("Average:", marks.mean())
print("Highest:", marks.max())
print("Lowest:", marks.min())
print("\nMarks >= 85:")
print(marks[marks >= 85])
Revision Questions
- What is a Pandas Series?
- How do you create a Series from a list?
- What is a custom index?
- What is the difference between
locandiloc? - Which methods calculate the sum and mean?
- How can you identify missing values?
Summary
A Pandas Series is a one-dimensional labelled data structure that stores values alongside an index. You can create one from a list or dictionary and provide custom index labels.
You learned to access values using loc and
iloc, inspect the Series using attributes,
perform calculations, filter entries, and identify or fill
missing values.