How pandas series works in Python? Best example

How pandas series works in Python? Best example
“`html

When working with data in Python, the pandas library is indispensable. One of its fundamental structures is the pandas.Series object, which acts as a one-dimensional labeled array. But how exactly does pandas.Series() work in Python? Let’s break it down with practical examples.

What Is a pandas Series?

A pandas.Series is essentially a one-dimensional array-like object that can hold various data types, including integers, floats, strings, and even objects. Each value in a Series has an associated index, which makes data retrieval efficient and intuitive.

Creating a pandas Series

Let’s start by creating a simple Series from a list:

import pandas as pd

data = [10, 20, 30, 40, 50]
series = pd.Series(data)
print(series)

The output will be:

0    10
1    20
2    30
3    40
4    50
dtype: int64

As you can see, the Series assigns an automatic index starting from 0, similar to a Python list.

Custom Index in pandas Series

One of the powerful features of a Series is using custom index labels. This makes data querying more meaningful.

data = [10, 20, 30, 40, 50]
index = ['a', 'b', 'c', 'd', 'e']
series = pd.Series(data, index=index)
print(series)

Output:

a    10
b    20
c    30
d    40
e    50
dtype: int64

Now, instead of using numerical indices, I can access values using these custom labels:

print(series['c'])  # Output: 30

Using Dictionaries to Create a Series

Another way to create a pandas Series is by using a dictionary:

data = {'Alice': 25, 'Bob': 30, 'Charlie': 35}
series = pd.Series(data)
print(series)

Output:

Alice      25
Bob        30
Charlie    35
dtype: int64

The dictionary keys become the Series index, making it easy to retrieve values:

print(series['Alice'])  # Output: 25

Operations on pandas Series

Since a pandas.Series supports vectorized operations, performing mathematical computations is simple:

series = pd.Series([1, 2, 3, 4, 5])
print(series * 10)

This will multiply each element in the Series by 10:

0    10
1    20
2    30
3    40
4    50
dtype: int64

Checking for Missing Values

A common challenge in data analysis is handling missing values. A pandas Series allows checking for NaN values using isna() or notna():

data = [10, 20, None, 40, 50]
series = pd.Series(data)

print(series.isna())  # Returns a boolean Series

Filtering Data in a Series

Series support conditional filtering, making it easy to extract specific values:

series = pd.Series([10, 20, 30, 40, 50])
filtered = series[series > 25]
print(filtered)

The output will display values greater than 25:

2    30
3    40
4    50
dtype: int64

Converting Series to Other Data Types

At times, converting a Series to another format is necessary. Here are a few conversions:

  • to_list() – Convert to list
  • to_dict() – Convert to dictionary
  • to_frame() – Convert to DataFrame format

Example:

series = pd.Series([10, 20, 30])
print(series.to_list())  # Output: [10, 20, 30]
print(series.to_dict())  # Output: {0: 10, 1: 20, 2: 30}

Head-to-Head Comparison: Series vs. NumPy Arrays

Let’s summarize key differences between pandas Series and NumPy arrays in table format:

Feature pandas.Series NumPy Array
Indexing Supports labeled indexing Only numeric indices
Operations Aligns on index Element-wise only
Handling Missing Data Supports NaN No built-in support

When to Use pandas Series?

Here are some common use cases for a pandas Series:

  1. When storing time series data
  2. For labeling data in machine learning
  3. When working with financial or stock data
  4. To perform quick mathematical calculations on single-column data

Conclusion

By now, you should have a solid understanding of how pandas Series works in Python. Whether it’s storing financial data, working with missing values, or performing analytics, pandas.Series is a powerful tool. If you are working with structured or semi-structured data, mastering Series will save you both time and effort.

“` Other interesting article: How pandas dataframe works in Python? Best example