liveBook · Manning

String

As explained in Pandas Workout: 200 exercises to make you a stronger data analyst

An immutable Python sequence of Unicode characters.

A string is an immutable sequence of Unicode characters. In pandas, string values become especially useful when a Series exposes the str accessor, which applies string methods element by element and returns a new Series. section 9.0

Common Series operations turn textual data into searchable, measurable, and normalized values:

  • Measure values with str.len(), select digit-only values with str.isdigit(), and search with str.contains(). section 9.0
  • Normalize text with str.lower() and str.strip(), then count results with value_counts(). section 9.2
  • Chain transformations, assign returned Series to new columns, or apply custom functions with Series.apply(). section 9.0 section 9.2

Cleaning and counting words

Read a small text file, split it into words, and create a Series. Normalize capitalization and punctuation before counting: s.str.lower().str.strip(string.punctuation).value_counts().head(10). The same Series can identify capitalized words with a regular expression or calculate average vowel counts with a custom function. section 9.2

Table 9.1 What you need to know (view table figure)

Concept

What is it?

Example

To learn more

s.explode

Returns a new series with each element on its own line

s.explode()

http://mng.bz/RxDP

str.contains

Returns a series of booleans, indicating which elements of the input series contain the target string

s.str.contains('a')

http://mng.bz/2D2X

str.get_dummies

Returns a data frame containing 1s and 0s based on a categorical series

s['country']. get_dummies (sep=';')

http://mng.bz/1q2g

str.index

Returns a series of integers, each indicating where the target string was found in the corresponding element of the input series

s.str.index('a')

http://mng.bz/PzEP

str.len

Returns a series of integers indicating the length of each element

s.str.len()

http://mng.bz/Jg0v

str.replace

Returns a series based on an existing series, replacing the first argument with the second

s.str.replace('a', 'e')

http://mng.bz/wv6Q

str.split

Returns a series in which each element is a list of strings; the argument specifies the delimiter used to perform the split

s.str.split(';')

http://mng.bz/qrD2

str.strip

Returns a series of Python strings without the argument’s characters on either side

s.str.strip('.!?')

http://mng.bz/7D2y

s.isin

Returns a boolean series indicating whether a value in s is an element of the argument

s.isin(['A', 'B', 'C'])

http://mng.bz/mVW2

i.intersection

Returns a new index object containing elements in two existing index objects

i.intersection(i2)

http://mng.bz/5w21

Use the method table to compare string operations by returned object, purpose, and example syntax. section 9.0

Pandas historically stored Python strings with object dtype, which could accidentally mix strings with other Python objects. StringDtype provides a text-specific alternative, paired with pd.NA for missing values. section 9.0

How Pandas Workout: 200 exercises to make you a stronger data analyst covers it

How deep it goes
Demonstrates chained transformations, regular-expression filtering, missing-value handling, and custom functions rather than string internals.
Examples
Uses word-frequency analysis, punctuation stripping, digit detection, regular-expression filtering, digit extraction, and vowel counting.
What it assumes
Assumes familiarity with Python strings and pandas Series while teaching the str accessor through executable exercises.
What you can do after
Apply it via pandas string methods to clean, inspect, filter, transform, and summarize textual Series.
How it connects
Connects Python strings with pandas Series, the str accessor, regular expressions, object dtype, StringDtype, and pd.NA.

Also explained in

Compare how 24 books explain string →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pandas Workout: 200 exercises to make you a stronger data analyst. The section links point to the parts of the book it draws on.