pandas basics · lesson 1 of 9
Parts in this lesson
Aimport pandas as pdthe usual way to load pandas
Bpd.Series(values, index=labels)an array with a label on each value
Cs.value_counts()how often each value appears
Ds[s > 30]2.5: a mask, now keeping labels

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 NaN comes from
  • Count how often each value appears with value_counts

An array with labels

Run the cells.

series.ipynb

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:

Two Series added by label. Two Series: Monday’s kilos and Tuesday’s. Each value has a label, its store. Tanauan sent nothing on Tuesday, and a new store, Rosario, did.423529383324monBatangasLipaTanauantueBatangasLipaRosario
1 of 4 Two Series: Monday’s kilos and Tuesday’s. Each value has a label, its store. Tanauan sent nothing on Tuesday, and a new store, Rosario, did.

Now run it.

two-days.ipynb
  • mon + tue gives four stores. Batangas and Lipa are in both, so they add up. Tanauan and Rosario are only in one, so their totals are NaN, missing.
  • The dtype became float64, because NaN is 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, no NaN, 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.

counts.ipynb
  • value_counts() counts each different value, most common first. The answer is a Series too: the values became the labels.
  • normalize=True gives 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

s = pd.Series([5, 8], index=["rice", "sugar"]). What is s["sugar"]?
a has labels Lipa and Rosario, b has Lipa and Tanauan. How many values does a + b have, and how many are NaN?
A column of store names has 500 rows. Which line shows how many rows each store has?

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.

All sources for Simple Data Analysis