1. Introduction to Series — Your One-Dimensional Data Ally
When performing data analysis, handling one-dimensional data can often feel tedious. Thankfully, the `Series` object in the Pandas library offers an elegant and powerful solution. Think of a Series as a single column of data — similar to one column in an Excel spreadsheet — with optional labels (indexes) that make data processing more intuitive and readable.
2. Creating a Series from a List — The Basics
To start using Series, first import the Pandas library:
import pandas as pd
Assume you have sales data for product A over a week stored in a list:
sales_data = [50, 30, 40, 45, 60, 70, 65]
You can create a Series like this:
s = pd.Series(sales_data) print(s)
By default, Pandas will assign an index starting from 0, such as 0, 1, 2, and so on. This helps you quickly organize and reference values.
3. Custom Indexing — Better Clarity and Labeling
To make your Series more meaningful, especially when dealing with labeled data (like product IDs), you can define your own index:
sales_data = [50, 35, 42] product_ids = ['P001', 'P002', 'P003'] s = pd.Series(sales_data, index=product_ids) print(s)
Now, instead of 0, 1, 2, you’ll see P001, P002, and P003 as indices — improving clarity when you read or present your data.
4. Accessing Values — By Position or Label
Accessing values from a Series is simple. You can use either the default index or your custom labels:
print(s[0]) # Access using default index print(s['P002']) # Access using custom label
This dual-access capability makes it flexible and user-friendly, ideal for a variety of data analysis scenarios.
5. Creating Series from Dictionaries — Direct and Expressive
You can also create a Series from a dictionary. The keys become the index, and the values become the data:
data = {'shirt': 100, 'pants': 80, 'shoes': 60} s = pd.Series(data) print(s)
This creates a Series like:
shirt 100 pants 80 shoes 60 dtype: int64
Need only part of the dictionary? Just pass a list of keys to the `index` parameter:
s = pd.Series(data, index=['shirt', 'pants']) print(s)
This lets you selectively include only the necessary data.
6. Conclusion: Make Series Your Data Companion
Pandas Series is a versatile and powerful tool for managing one-dimensional data. Whether you’re initializing it from lists or dictionaries, customizing its index, or accessing elements flexibly, Series makes your data analysis process cleaner, faster, and more intuitive.
7. Demo Video
You can watch the following demo video by select the subtitle to your preferred subtitle language.