liveBook · Manning

First_digits

As explained in Statistics Every Programmer Needs

first_digits is an API function that analyzes the distribution of leading digits and compares it with an expected Benford distribution.

first_digits analyzes leading-digit frequencies against an expected Benford distribution. It accepts a cleaned leading-digit variable and, with digs = 1, evaluates the first digit. section 12.4.1

Prepare the input and interpret the returned comparison:

  • Extract the first character from each original value, inspect unusual results, retain digits 1 through 9, and convert the field to integers. section 12.4.1 section 12.4.3
  • Call bf.first_digits(first_digit, digs = 1) to obtain a nine-row data frame containing digits, counts, observed percentages, and expected percentages. section 12.4.1
  • Use the automatically displayed bar-line chart to compare observed frequencies with the perfect Benford distribution. section 12.4.1

Payment amounts

For utility payments, extract the first character from Amount, remove minus signs and zero values, convert the remaining digits to integers, and run bf.first_digits(). The resulting distribution has leading digits 1, 2, and 3 in 59% of records, compared with 60.2% under a perfect Benford distribution. section 12.4.3

>>> import benford as bf
>>> bf_street_addresses = bf.first_digits(street_addresses.first_digit, 
>>>                                            digs = 1)

Run the compact benford-library call after creating and validating the first_digit field. section 12.4.1

How Statistics Every Programmer Needs covers it

How deep it goes
Explains preprocessing, output columns, and visual comparison but defers exact statistical measurements beyond inspection.
Examples
Uses street addresses, world population counts, and utility payment amounts as naturally occurring numeric datasets.
What it assumes
Assumes basic pandas data preparation and teaches leading-digit extraction, filtering, conversion, and comparison from the raw values.
What you can do after
Evaluate leading-digit conformity by preparing a pandas field and applying the benford library's first_digits function.
How it connects
Connects leading digits, Benford's law, data cleaning, data frames, and data visualization.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Statistics Every Programmer Needs. The section links point to the parts of the book it draws on.