Pandas Series

A Pandas Series is a one-dimensional data structure used to store values with labels called indexes. It is similar to a single column in an Excel worksheet. In this chapter, we will learn how to create a Series, access its values, and understand its labels with simple Python examples.

1. What is a Pandas Series?

A Series is a one-dimensional labeled array that can store different types of data, such as numbers, text, and Boolean values.

Each value in a Series has an index. By default, Pandas assigns index numbers starting from 0.

Example:

import pandas as pd

data = [10, 20, 30, 40]

myseries = pd.Series(data)

print(myseries)

Output:

0    10
1    20
2    30
3    40
dtype: int64

Explanation:

  • pd.Series() creates a Pandas Series.
  • data contains four values.
  • Pandas automatically assigns indexes 0, 1, 2, 3.
  • dtype: int64 indicates the data type of the values.

2. Creating a Pandas Series

You can create a Series using a Python list, tuple, or dictionary.

Creating a Series Using a List

A Python list is one of the most common ways to create a Series.

Example:

import pandas as pd

marks = [85, 90, 75, 95]

result = pd.Series(marks)

print(result)

Output:

0    85
1    90
2    75
3    95
dtype: int64

Creating a Series Using a Tuple

You can also create a Series from a Python tuple.

Example:

import pandas as pd

prices = (100, 200, 300, 400)

result = pd.Series(prices)

print(result)

Output:

0    100
1    200
2    300
3    400
dtype: int64

Creating a Series Using a Dictionary

When you create a Series using a dictionary, its keys become the index labels and its values become the Series data.

Example:

import pandas as pd

marks = {
    "Rahul": 85,
    "Priya": 90,
    "Amit": 75
}

result = pd.Series(marks)

print(result)

Output:

Rahul    85
Priya    90
Amit     75
dtype: int64

Explanation:

  • Dictionary keys become index labels.
  • Dictionary values become Series values.
  • The original labels are preserved instead of using default numeric indexes.

3. Labels in Pandas Series

Every value in a Pandas Series has an index label. By default, Pandas creates numeric labels starting from 0.

You can also assign your own labels using the index parameter.

Example:

import pandas as pd

marks = [85, 90, 75]

result = pd.Series(marks, index=["Rahul", "Priya", "Amit"])

print(result)

Output:

Rahul    85
Priya    90
Amit     75
dtype: int64

Explanation:

  • index assigns custom labels to the Series.
  • Rahul represents the first value, 85.
  • Priya represents the second value, 90.
  • Amit represents the third value, 75.

4. Accessing Pandas Series Values Using Labels

You can access a Series value using its custom index label.

Example:

import pandas as pd

marks = pd.Series(
    [85, 90, 75],
    index=["Rahul", "Priya", "Amit"]
)

print(marks["Priya"])

Output:

90

Explanation:

  • marks["Priya"] searches for the index label Priya.
  • Pandas returns the value associated with that label.
  • The result is 90.

5. Accessing Pandas Series Values Using Position

You can also access values based on their position using square brackets with numeric indexes when the Series has the default integer index.

Example:

import pandas as pd

marks = pd.Series([85, 90, 75, 95])

print(marks[0])
print(marks[2])

Output:

85
75

Explanation:

  • marks[0] returns the first value.
  • marks[2] returns the third value.
  • Position counting starts from 0.

Note: When a Series has custom labels, use .iloc[] for position-based access and .loc[] for label-based access. These methods will be covered in detail in later chapters.

6. Creating a Series Using the index Parameter

The index parameter allows you to assign custom labels to Series values.

Example:

import pandas as pd

sales = pd.Series(
    [5000, 7000, 9000],
    index=["January", "February", "March"]
)

print(sales)

Output:

January     5000
February    7000
March       9000
dtype: int64

Explanation:

  • The first value 5000 gets the label January.
  • The second value 7000 gets the label February.
  • The third value 9000 gets the label March.

This is useful when storing monthly sales, yearly revenue, or department-wise information.

7. Creating a Series Using the name Parameter

The name parameter assigns a name to the Series.

Example:

import pandas as pd

marks = pd.Series(
    [85, 90, 75],
    index=["Rahul", "Priya", "Amit"],
    name="Student Marks"
)

print(marks)

Output:

Rahul    85
Priya    90
Amit     75
Name: Student Marks, dtype: int64

Explanation:

  • name="Student Marks" assigns a name to the Series.
  • Pandas displays this name in the Series output.
  • The name is also available through the .name attribute.

8. Creating a Series Using the dtype Parameter

The dtype parameter allows you to specify the data type of Series values.

Example:

import pandas as pd

numbers = pd.Series([10, 20, 30], dtype="float64")

print(numbers)

Output:

0    10.0
1    20.0
2    30.0
dtype: float64

Explanation:

  • dtype="float64" specifies that the Series should store floating-point values.
  • Integer values are converted into floating-point values.
  • The output displays float64 as the data type.

Student Task

Create the following Pandas Series and complete these tasks:

import pandas as pd

employee_salary = [25000, 30000, 45000, 50000]
  1. Create a Series using the given list.
  2. Assign employee names as custom indexes.
  3. Access the salary of the second employee using its label.
  4. Access the third salary using its position.
  5. Create another Series using a dictionary of employee names and salaries.
  6. Create a Series with the name Employee Salary.
  7. Create a Series using the dtype parameter with float64.

Key Takeaways

  • A Pandas Series is a one-dimensional labeled data structure.
  • pd.Series() creates a Series.
  • A Series can be created using lists, tuples, and dictionaries.
  • Pandas automatically assigns numeric indexes when custom indexes are not provided.
  • The index parameter assigns custom labels.
  • The name parameter assigns a name to the Series.
  • The dtype parameter specifies the data type of Series values.

Frequently Asked Questions (FAQs)

1. What is a Pandas Series?

A Pandas Series is a one-dimensional labeled data structure used to store values with indexes.

2. How do you create a Series in Pandas?

Use pd.Series() to create a Series from a list, tuple, dictionary, or other supported data.

3. What is the default index of a Pandas Series?

By default, Pandas assigns integer indexes starting from 0.

4. Can we assign custom indexes to a Pandas Series?

Yes, you can use the index parameter to assign custom labels.

5. What is the difference between a list and a Pandas Series?

A list is a basic Python collection, whereas a Pandas Series provides labeled indexes and additional data analysis features.

6. Can a Pandas Series store text values?

Yes, a Pandas Series can store text, numbers, Boolean values, and other supported data types.

7. What is the use of the dtype parameter in a Pandas Series?

The dtype parameter specifies the data type of the values stored in a Series.

8. What is the use of the name parameter in a Pandas Series?

The name parameter assigns a descriptive name to a Series.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top