Series
Last updated
pandas is the library most data analysis in Python is done with. Its tables are made of columns, and each column is a Series: a NumPy array with a label on every value. In this lesson you'll make Series, do maths with them, count values in them, and see what the labels do when two Series meet.
By the end of this lesson you can
- Make a Series with an index, and pick values from it by label
- Do maths and filtering on a Series as on a NumPy array
- Explain how pandas lines up two Series by label, and where
NaNcomes from - Count how often each value appears with
value_counts
An array with labels
Run the cells.
import pandas as pd loads pandas under its usual short name, as np is for NumPy.
A Series is two things side by side:
- values: a NumPy array.
kilos.to_numpy()gives it back,array([42, 35, 29]). - an index: a label for each value. Here, store names.
kilos["Lipa"]picks a value by its label, so you don't have to remember that Lipa is position 1.
Everything from Stage 2 still works. kilos.mean() is an aggregation, and kilos[kilos > 30] is a mask. The difference is in the answer: the mask keeps the labels, so you can see which stores sold more than 30 kg, not just how much.
The name="kilos" is optional. It becomes the column name when a Series goes into a table, in lesson 3.2.
Lining up by label
When two Series meet, pandas matches their values by label. That's the main thing it adds to NumPy, and it can surprise you. Step through:
Now run it.
mon + tuegives four stores. Batangas and Lipa are in both, so they add up. Tanauan and Rosario are only in one, so their totals areNaN, missing.- The dtype became
float64, becauseNaNis a float (lesson 2.3). mon.add(tue, fill_value=0)says "count a missing day as 0". Now Rosario is 24 and Tanauan 29. Whether 0 is right is your call: if Tanauan was closed, yes; if its figure was lost, no.mon.to_numpy() + tue.to_numpy()throws the labels away. Plain arrays add by position, and the third answer, 53, is Tanauan's Monday plus Rosario's Tuesday. No error, noNaN, and wrong.
So a NaN after adding two Series isn't a bug. It's pandas telling you the labels didn't all match. Look at which ones, and decide what they should be.
Counting values
One question you'll ask of almost every column: how often does each value appear? Run the cells.
value_counts()counts each different value, most common first. The answer is a Series too: the values became the labels.normalize=Truegives shares instead of counts: Lipa is half the rows.nunique()says how many different values there are.
In project 1.P you found a store spelled two ways with a set comprehension. value_counts() finds it in one line, and says how many rows each spelling has.
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- pandas: Intro to data structures, Series: what a Series is, and alignment by label
- pandas: pandas.Series.add: adding with fill_value
- pandas: pandas.Series.value_counts: counts, and normalize for shares
- McKinney, Data Structures for Statistical Computing in Python, SciPy 2010: the paper that introduced pandas, and why it aligns by label