0Pricing
Pandas & NumPy Academy · Lesson

Useful Series Methods

Use describe(), value_counts(), unique(), and map() to summarise and transform a Series quickly.

Useful Series Methods is a free Pandas & NumPy Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Pandas & NumPy Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Quick Summary with describe()

s.describe() generates a statistical summary in a single call: count, mean, standard deviation, min, 25th percentile (Q1), median (Q2), 75th percentile (Q3), and max. For string Series it returns count, unique, top, and frequency instead. It is the fastest way to get a feel for a column's distribution when first exploring a dataset.

import pandas as pd

ages = pd.Series([25, 32, 19, 45, 28, 31, 22, 40])
print(ages.describe())
# count     8.000000
# mean     30.250000
# std       8.406...
# min      19.000000
# 25%      24.250000
# 50%      29.500000
# 75%      34.750000
# max      45.000000

value_counts() for Frequency Tables

s.value_counts() returns a new Series with the unique values as the index and their occurrence counts as the data, sorted descending by count. Pass normalize=True to get proportions instead of raw counts. This is the first thing to run on a categorical column to understand its distribution.

import pandas as pd

colors = pd.Series(['red', 'blue', 'red', 'green', 'blue', 'red'])
print(colors.value_counts())
# red      3
# blue     2
# green    1
print(colors.value_counts(normalize=True).round(2))
# red      0.50  blue  0.33  green  0.17

unique() and nunique()

s.unique() returns a NumPy array of distinct values in their first-occurrence order (unlike value_counts which sorts by frequency). s.nunique() returns the count of distinct values as an integer — the cardinality of the column. Both skip NaN by default. Use nunique(dropna=False) to count NaN as a distinct value.

import pandas as pd

s = pd.Series(['apple', 'banana', 'apple', 'cherry', 'banana'])
print(s.unique())    # ['apple' 'banana' 'cherry']
print(s.nunique())   # 3

map() for Element-Wise Transformation

s.map() applies a function, a dict, or another Series to every element. When you pass a dict, each value is replaced by the corresponding dict value, and values not in the dict become NaN. When you pass a function, it is applied element-wise. map is ideal for recoding categorical labels or applying lookup tables.

import pandas as pd

grades = pd.Series(['A', 'B', 'C', 'A', 'B'])
gpa = {'A': 4.0, 'B': 3.0, 'C': 2.0}
print(grades.map(gpa))
# 0    4.0
# 1    3.0
# 2    2.0
# 3    4.0
# 4    3.0

apply() for Custom Functions

s.apply(func) applies a Python callable to each element and collects the results. Unlike map(), it is designed for more complex functions that may return non-scalar objects. For simple element-wise transformations, prefer map() or a vectorized expression; use apply() when the logic is too complex to vectorize directly.

import pandas as pd

sentences = pd.Series(['Hello world', 'Pandas is great', 'Data science'])
word_counts = sentences.apply(lambda x: len(x.split()))
print(word_counts)
# 0    2
# 1    3
# 2    2

sort_values() and sort_index()

s.sort_values() returns the Series sorted by data values in ascending order (pass ascending=False for descending). s.sort_index() sorts by index labels. Neither modifies the original Series by default. Pass inplace=True to modify in place (less recommended in modern Pandas code that chains operations).

import pandas as pd

s = pd.Series([30, 10, 50, 20], index=['d', 'a', 'c', 'b'])
print(s.sort_values())
# a    10
# b    20
# d    30
# c    50
print(s.sort_index())
# a    10
# b    20
# c    50
# d    30

isin() for Membership Testing

s.isin(values) returns a boolean Series that is True wherever the element is in the provided list or set. This is more readable than multiple | conditions and is significantly faster for large sets. It is commonly used to filter rows belonging to a specific group or to flag records matching a lookup list.

import pandas as pd

countries = pd.Series(['DE', 'US', 'FR', 'UK', 'US', 'DE'])
nordics = ['DE', 'FR']
print(countries.isin(nordics))
# 0     True  1    False  2     True  3    False  4    False  5     True
print(countries[countries.isin(nordics)])

between() for Range Filtering

s.between(left, right, inclusive='both') returns a boolean Series that is True where the value falls within the range [left, right]. Both endpoints are inclusive by default. It is more concise than writing (s >= left) & (s <= right) and clearly communicates intent. Useful for filtering numeric ranges like age groups or price bands.

import pandas as pd

scores = pd.Series([45, 72, 83, 91, 55, 68])
pass_mask = scores.between(60, 100)
print(pass_mask)
# 0    False  1    True  2    True  3    True  4    False  5    True
print(scores[pass_mask].values)  # [72 83 91 68]

head() and tail() for Quick Inspection

s.head(n) returns the first n elements (default 5) and s.tail(n) returns the last n. These are used constantly when exploring data to peek at the start and end of a long Series without printing thousands of rows. They return a new Series (a slice), so they do not modify the original.

import pandas as pd

s = pd.Series(range(100))
print(s.head(3))
# 0    0
# 1    1
# 2    2
print(s.tail(3))
# 97    97
# 98    98
# 99    99

rename() for Changing the Name

s.rename('new_name') returns a new Series with a different name attribute. To rename the index labels, pass a dict or function: s.rename(index={'old': 'new'}). Renaming is important before combining Series into a DataFrame, because the Series name becomes the column header. Always rename rather than reassign raw values.

import pandas as pd

s = pd.Series([1, 2, 3], index=['a', 'b', 'c'], name='original')
renamed = s.rename('updated').rename(index={'a': 'x', 'b': 'y', 'c': 'z'})
print(renamed)
# x    1
# y    2
# z    3
# Name: updated

idxmin() and idxmax()

s.idxmin() returns the index label of the minimum value and s.idxmax() returns the label of the maximum value. This is more useful than argmin/argmax when the index has meaningful labels like dates or product names — you get the actual identifier, not just a position number.

import pandas as pd

scores = pd.Series({'Alice': 88, 'Bob': 73, 'Carol': 95, 'Dan': 60})
print('Best:', scores.idxmax(), scores.max())   # Carol 95
print('Worst:', scores.idxmin(), scores.min())  # Dan   60

Quick Check

Test your understanding of useful Series methods from this lesson.

Lesson Recap

In this lesson you learned: describe() provides a statistical summary in one call, value_counts() builds a frequency table of unique values, and map() translates values using a dict while apply() runs a custom function on each element. Next up we start working with DataFrames — the two-dimensional workhorse of Pandas data analysis.

Frequently asked questions

Is the “Useful Series Methods” lesson free?

Yes — the full text of “Useful Series Methods” is free to read here on the web, and the Pandas & NumPy Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Pandas & NumPy Academy course, upgrade to CoddyKit PRO.

What will I learn in “Useful Series Methods”?

Use describe(), value_counts(), unique(), and map() to summarise and transform a Series quickly. You practise Pandas & NumPy Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Pandas & NumPy Academy?

No prior experience is required. Pandas & NumPy Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Useful Series Methods” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Pandas & NumPy Academy lesson?

Yes. Every Pandas & NumPy Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Creating a Series
  2. Label-Based and Position-Based Access
  3. Vectorized Operations on Series
  4. Useful Series Methods
← Back to Pandas & NumPy Academy