Overview

1 Series

This chapter introduces pandas through the lens of the Series, showing that a data frame is essentially a collection of one-dimensional series objects. It emphasizes that many familiar methods work on both series and data frames, but on a data frame they usually produce one result per column. The chapter also highlights the importance of indexes for selecting, aligning, and transforming data, and it introduces common conventions for naming pandas objects as well as useful reference tools for working in Jupyter and with core Python and NumPy features.

Most of the chapter is built around hands-on exercises that teach the basics of creating series, setting indexes, and using aggregation methods such as mean, max, min, standard deviation, and descriptive summaries. The reader learns how to generate reproducible random data with NumPy generators, how to attach meaningful string indexes, and how to slice series by position or label using ordinary slicing, .iloc, and .loc. The text also explains subtle but important details such as the inclusive endpoint behavior of .loc, the difference between population and sample standard deviation, and how data type choices affect memory use and correctness.

Later examples build on these fundamentals to show pandas’ vectorized behavior, where arithmetic and comparisons operate elementwise and align by index rather than by position. This leads to practical techniques for scaling scores, transforming values through broadcasting, and selecting or replacing values with boolean mask indexes. The chapter closes by showing how to count values, group data into categories with value_counts and pd.cut, and work with real-world CSV data. Across all of these examples, the main lesson is that understanding series, indexes, and data types unlocks most everyday pandas workflows.

Each of a data frame’s columns is a series.
Invoking a series method (such as mean) on a data frame often returns one value for each column.
Retrieving slices from our series
When we add two series together, the result is a new series—the result of adding elements at the same index.
Vectorized operations work using the index, not the position.
Operations involving a series and a scalar value result in broadcasting the operation, resulting in a new series.
Graphical depiction of dividing the series by 10, converting to np.int8
Graphical depiction of turning the series into strings, retrieving the item at index –2, and replacing NaN with 0

Summary

In this chapter, we saw that a pandas series provides powerful tools to analyze data. Whether it’s the index, reading data from files, calculating descriptive statistics, retrieving values via fancy indexing, or even categorizing our data via numeric boundaries, we can do a lot. In the next chapter, we’ll expand our reach to look at data frames, the two-dimensional data structures that most people think of when they work with pandas.

FAQ

What is a pandas Series, and how does it relate to a DataFrame?A Series is a one-dimensional labeled data structure. In pandas, each column of a DataFrame is a Series, so you can think of a DataFrame as a collection of Series objects.
How do you create a Series from a list of values?You create a Series by calling Series() and passing an iterable such as a Python list or NumPy array, for example: s = Series([10, 20, 30, 40, 50]).
How can you create random integers for a Series in pandas?Use NumPy’s random number generator: create one with np.random.default_rng(seed), then call g.integers(low, high, size). For example, g.integers(70, 101, 10) creates 10 integers from 70 to 100.
How do you set the index of a Series to month names?You can assign a list of labels to s.index, or pass the labels when creating the Series with the index= parameter. For example: s.index = 'Sep Oct Nov Dec Jan Feb Mar Apr May Jun'.split().
What does s.mean() return for a numeric Series?s.mean() returns the arithmetic mean of the Series values. Even if the values are integers, the result is returned as a float.
What is the difference between .iloc and .loc?.iloc retrieves values by numeric position, while .loc retrieves values by index label. With .loc, slice endpoints are inclusive; with .iloc, the stop value is excluded like normal Python slicing.
How can you get the first or last five elements of a Series?Use s.head() for the first five elements and s.tail() for the last five elements. You can also pass an explicit number, such as s.head(5) or s.tail(5).
What does it mean when a Series operation is “broadcast”?Broadcasting means pandas applies an operation between a Series and a scalar to every element in the Series, returning a new Series. For example, s + 3 adds 3 to every value in s.
How do you calculate descriptive statistics for a Series?Use s.describe(). It returns summary statistics such as count, mean, standard deviation, minimum, 25% quantile, median (50%), 75% quantile, and maximum.
How do you replace missing values or categorize values in a Series?Use s.fillna(value) to replace missing values, and pd.cut() to convert numeric values into labeled categories based on bins. For example, pd.cut(s, bins=[0, 2, 10, s.max()], labels=['short', 'medium', 'long']).

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Pandas Workout Pandas Workout ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Pandas Workout Pandas Workout ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Pandas Workout Pandas Workout ebook for free