++++Data Science
May 2026×Notebook lesson
Notebook converted from Jupyter for blog publishing.
00-Series
Driptanil DattaSoftware Developer
Series
The first main data type we will learn about for pandas is the Series data type. Let's import Pandas and explore the Series object.
A Series is very similar to a NumPy array (in fact it is built on top of the NumPy array object). What differentiates the NumPy array from a Series, is that a Series can have axis labels, meaning it can be indexed by a label, instead of just a number location. It also doesn't need to hold numeric data, it can hold any arbitrary Python Object.
Let's explore this concept through some examples:
Imports
import numpy as np
import pandas as pdCreating a Series from Python Objects
help(pd.Series)STDOUT
MORE
Help on class Series in module pandas.core.series:
class Series(pandas.core.base.IndexOpsMixin, pandas.core.generic.NDFrame)
| One-dimensional ndarray with axis labels (including time series).
| Index and Data Lists
We can create a Series from Python lists (also from NumPy arrays)
myindex = ['USA','Canada','Mexico']mydata = [1776,1867,1821]myser = pd.Series(data=mydata)myserRESULT
0 1776
1 1867
2 1821
dtype: int64pd.Series(data=mydata,index=myindex)RESULT
USA 1776
Canada 1867
Mexico 1821
dtype: int64ran_data = np.random.randint(0,100,4)ran_dataRESULT
array([39, 35, 37, 23])names = ['Andrew','Bobo','Claire','David']ages = pd.Series(ran_data,names)agesRESULT
Andrew 39
Bobo 35
Claire 37
David 23
dtype: int32From a Dictionary
ages = {'Sammy':5,'Frank':10,'Spike':7}agesRESULT
{'Frank': 10, 'Sammy': 5, 'Spike': 7}pd.Series(ages)RESULT
Sammy 5
Frank 10
Spike 7
dtype: int64Key Ideas of a Series
Named Index
# Imaginary Sales Data for 1st and 2nd Quarters for Global Company
q1 = {'Japan': 80, 'China': 450, 'India': 200, 'USA': 250}
q2 = {'Brazil': 100,'China': 500, 'India': 210,'USA': 260}# Convert into Pandas Series
sales_Q1 = pd.Series(q1)
sales_Q2 = pd.Series(q2)sales_Q1RESULT
Japan 80
China 450
India 200
USA 250
dtype: int64# Call values based on Named Index
sales_Q1['Japan']RESULT
80# Integer Based Location information also retained!
sales_Q1[0]RESULT
80Be careful with potential errors!
# Wrong Name
# sales_Q1['France']# Accidental Extra Space
# sales_Q1['USA ']# Capitalization Mistake
# sales_Q1['usa']Operations
# Grab just the index keys
sales_Q1.keys()RESULT
Index(['Japan', 'China', 'India', 'USA'], dtype='object')# Can Perform Operations Broadcasted across entire Series
sales_Q1 * 2RESULT
Japan 160
China 900
India 400
USA 500
dtype: int64sales_Q2 / 100RESULT
Brazil 1.0
China 5.0
India 2.1
USA 2.6
dtype: float64Between Series
# Notice how Pandas informs you of mismatch with NaN
sales_Q1 + sales_Q2RESULT
MORE
Brazil NaN
China 950.0
India 410.0
Japan NaN
USA 510.0# You can fill these with any value you want
sales_Q1.add(sales_Q2,fill_value=0)RESULT
MORE
Brazil 100.0
China 950.0
India 410.0
Japan 80.0
USA 510.0That is all we need to know about Series, up next, DataFrames!