Back to Python
2025-12-106 min read

Pandas Series (Python Programming)

Learn Pandas Series (Python Programming) step by step with clear examples and exercises.

Why This Matters

Understanding Pandas Series is essential for anyone working with data in Python. It allows you to handle complex datasets efficiently, making it an indispensable tool for data analysis, machine learning, and data visualization tasks. Mastering Pandas Series can help you stand out in job interviews, solve real-world problems, and avoid debugging headaches.

Prerequisites

To follow this lesson, you should have a basic understanding of Python programming and its syntax. Familiarity with data structures like lists and dictionaries is also important. If you're new to Python, consider brushing up on those topics before diving into Pandas Series. Additionally, it would be beneficial to have some experience working with large datasets or arrays in other programming languages.

Important Python Concepts for Pandas Series:

  1. Data Types: Python supports various data types such as integers, floating-point numbers, strings, lists, and dictionaries. Understanding these will help you create and manipulate Pandas Series effectively.
  2. Functions: Python has a rich library of built-in functions that can be used to perform various operations on data. Familiarity with these functions is crucial when working with Pandas Series.
  3. Control Structures: Python's control structures, including loops and conditional statements, enable you to iterate over and manipulate data in your series more effectively.

Core Concept

A Pandas Series is a one-dimensional labeled array capable of holding any data type (integers, floating-point numbers, strings, etc.). It's similar to a list or an array in other programming languages but with additional features like indexing and labeling that make it more versatile for handling complex datasets.

Creating a Pandas Series

To create a Pandas Series, you first need to import the pandas library:

import pandas as pd

Then, you can create a series using the Series() function and passing a list or dictionary as an argument. Here's an example of creating a series from a list:

Creating a Pandas Series from a list

s = pd.Series([1, 2, 3, 4, 5])

print(s)


Output:

0 1

1 2

2 3

3 4

4 5

dtype: int64


You can also create a series from a dictionary:

Creating a Pandas Series from a dictionary

data = {'Name': ['John', 'Anna', 'Peter'], 'Age': [28, 24, 35]}

s = pd.Series(data)

print(s)


Output:

Name John

Age 28

Name Anna

Age 24

Name Peter

Age 35

dtype: object


### Accessing and Modifying Series Data

To access the data in a series, you can use indexing. By default, Pandas Series uses integer indices starting from 0. Here's an example of accessing and modifying data:

Accessing and modifying data in a Pandas Series

s = pd.Series([1, 2, 3, 4, 5])

print("Original series:", s)

Accessing the third element (index 2)

print("Third element:", s[2])

Modifying the second element to 99

s[1] = 99

print("Modified series:", s)


Output:

Original series: 0 1

1 2

2 3

3 4

4 5

dtype: int64

Third element: 3

Modified series: 0 1

1 99

2 3

3 4

4 5

dtype: int64


### Handling Missing Data

When working with real-world datasets, you may encounter missing data (NaN values). Pandas provides several functions for handling missing data, such as `dropna()`, `fillna()`, and `interpolate()`. Make sure to use them when necessary.

#### Handling Missing Data Examples:

1. Dropping rows with missing data:

Dropping rows with missing data

s = pd.Series([1, 2, None, 4, 5])

print("Original series:", s)

clean_series = s.dropna()

print("Clean series without missing data:", clean_series)


Output:

Original series: 0 1

1 2

2 NaN

3 4

4 5

dtype: float64

Clean series without missing data: 0 1

1 2

3 4

4 5

dtype: float64


2. Filling missing data with a specific value:

Filling missing data with a specific value (0 in this example)

s = pd.Series([1, 2, None, 4, 5])

print("Original series:", s)

filled_series = s.fillna(0)

print("Filled series with missing data replaced by 0:", filled_series)


Output:

Original series: 0 1

1 2

2 NaN

3 4

4 5

dtype: float64

Filled series with missing data replaced by 0: 0 1

1 2

2 0

3 4

4 5

dtype: float64

Worked Example

Let's use a Pandas Series to analyze the ages of students in a class.

import pandas as pd

List of student ages

ages = [18, 20, 19, 21, 17, 23]

Create a Pandas Series from the list

student_series = pd.Series(ages)

print("Student ages series:", student_series)

Calculate the average age

average_age = student_series.mean()

print("Average age:", average_age)


Output:

Student ages series: 0 18

1 20

2 19

3 21

4 17

5 23

dtype: int64

Average age: 19.8

Common Mistakes

Forgetting to import pandas

Remember to start your script by importing the pandas library:

import pandas as pd

Misunderstanding indexing

Pandas Series uses integer indices starting from 0. Be careful when accessing or modifying elements, and remember that you can use negative indexing to access elements from the end of the series.

Not handling missing data correctly

When working with real-world datasets, you may encounter missing data (NaN values). Pandas provides several functions for handling missing data, such as dropna(), fillna(), and interpolate(). Make sure to use them when necessary.

Using the wrong indexing method

Instead of using [] for indexing, consider using iloc for integer-based indexing or loc for label-based indexing. This can help avoid confusion and ensure more robust indexing.

Practice Questions

  1. Create a Pandas Series from the following list: [4, 7, 2, 9, 5, 1].
  2. Modify the second and fourth elements of the series created in question 1 to 100 and 200, respectively.
  3. Calculate the sum of the numbers in a Pandas Series containing [3, 6, 9, 12, 15].
  4. Given a Pandas Series containing student names and scores: ['Alice', 85; 'Bob', 78; 'Charlie', 92], write code to calculate the average score.
  5. Write code to create a Pandas Series with non-integer indices, where the indices are the students' names from question 4 and the values are their scores.
  6. Given a Pandas Series containing a list of temperatures in degrees Celsius: [20, 22, 18, 25, 30], write code to convert the temperatures to Fahrenheit and create a new series with the converted values.
  7. Write code to calculate the standard deviation of a Pandas Series containing the ages of students from question 1.

FAQ

What happens if I try to access an index that doesn't exist in my Pandas Series?

If you try to access an index that doesn't exist, Pandas will return a NaN (Not-a-Number) value. To avoid this, always check the length of your series before accessing indices, or use iloc instead of [] for more robust indexing.

Can I create a Pandas Series with non-integer indices?

Yes! You can create a Pandas Series with any type of indices, as long as they are unique and hashable. To do this, simply pass your list or dictionary to the Series() function as usual.

How can I handle missing data in my Pandas Series?

Pandas provides several functions for handling missing data, such as dropna(), fillna(), and interpolate(). Make sure to use them when necessary.

What is the difference between [] and iloc for indexing a Pandas Series?

[] is used for label-based indexing, while iloc is used for integer-based indexing. Using iloc can help avoid confusion and ensure more robust indexing.

Pandas Series (Python Programming) | Python | XQA Learn