liveBook · Manning

Str.isdigit

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

str.isdigit is a string-accessor method that returns whether each string consists solely of digit characters.

str.isdigit checks the content of every string in a pandas Series through the .str accessor. It returns a same-length Boolean Series: True only when an element contains digit characters exclusively; empty strings, signs, and decimal points produce False. section 9.0 section 5.6

Filter values that contain only digits

Given s = Series('this is a test 123 456'.split()), run s.str.isdigit() to produce Boolean results, then use s.loc[s.str.isdigit()] to keep only 123 and 456. The Boolean Series shares s's index, so it works directly as a mask. section 9.0

Clean an age column

After removing missing ages, use df = df[df['age'].str.isdigit()] to discard rows whose age text cannot become an integer. This is suitable for ordinary ages, but it is not a general numeric test: values such as -2 or 3.5 fail. section 5.6

For broader numeric conversion, pd.to_numeric(..., errors='coerce') can replace this preliminary check by converting valid values and turning unconvertible values into NaN. Either approach still requires checking implausible results, such as an age of 9,394. section 5.6

s.str.isdigit()

Inspect the concise Series expression that applies isdigit elementwise. section 9.0

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

How deep it goes
Defines return behavior and demonstrates masking, but stops short of general numeric parsing and delegates broader conversion to pd.to_numeric.
Examples
Demonstrates filtering split words and removing nondigit ages from a data frame.
What it assumes
Assumes familiarity with pandas Series and Boolean indexing while explaining the string accessor from its elementwise behavior.
What you can do after
Diagnose and filter string values that cannot be treated as digit-only integers.
How it connects
Connects string accessors, Boolean indexing, missing-data cleaning, integer conversion, and numeric validation.

Also explained in

Compare how 2 books explain str.isdigit →

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.