Cycle 2 Formula Sheet

DATA SCIENCE · CYCLE 2 · REFERENCE

Formula Sheet

Each tool three ways: by hand, in Google Sheets, and in pandas. The Sheets formulas read column B, rows 2 to 33, the way the class file does; change the range to fit your file.

Counting

What you wantIn SheetsIn pandasFrom
Rows of data after an import=COUNTA(A:A)-1: the filled cells in column A, minus the header rowlen(df)L15, L19b
How many filled cells=COUNTA(A2:A33)L16
How many numbers=COUNT(B2:B33).count()L16, L19b
How many blanks=COUNTBLANK(B2:B33)L16
How many different values=COUNTUNIQUE(A2:A33).nunique()L16
Keep only the numbersFix "12 cm" and blanks by handpd.to_numeric(..., errors="coerce")L19b
Drop the blanksFilter the blank rows out.dropna()L21

A valid column has as many numbers as filled cells: =COUNT matches =COUNTA. To take out duplicate rows, work on a copy: Data > Data cleanup > Remove duplicates (L16).

Center and Spread

What you wantBy handIn SheetsIn pandasFrom
MeanAdd, then divide by how many.=AVERAGE(B2:B33).mean()L17, L19b
MedianPut them in order; the middle value. Two middles: their mean.=MEDIAN(B2:B33).median()L17, L19b
HistogramCount how many values fall in each range.Click the column letter. Insert > Chart. On Setup, set Chart type to Histogram chart.L17
Smallest, largestThe first and last in order.=MIN(B2:B33), =MAX(B2:B33).max() for the largestL18, L19b
Q1, Q3The median of each half. Odd count: the median is in both halves.=QUARTILE(B2:B33,1), =QUARTILE(B2:B33,3).quantile(0.25), .quantile(0.75)L18
IQRQ3 − Q1.Q3’s cell minus Q1’sL18
SDThe six steps below.=STDEV(B2:B33).std()L19, L19b

The SD by Hand

  1. Find the mean.
  2. Take the mean away from each value.
  3. Square each answer, so none is negative.
  4. Add the squares.
  5. Divide by n − 1, one less than how many values.
  6. Take the square root.

Sheets’ =STDEV and pandas’ .std() divide by n − 1 too, so all three give the same number. A small SD means the values are consistent.

Groups

What you wantIn SheetsIn pandasFrom
Keep only some rowsData > Create a filter. Click the filter icon in the column’s header. Clear, tick what you want, OK. When you are done: Data > Remove filter.df[df["grade"] == "9"]L22
One group’s mean=AVERAGEIF(DATA!C:C,9,DATA!J:J).mean() on the rows you keptL22
One group’s count=COUNTIF(CLEAN!A:A,A7)len( ) of the rows you keptL22
Every group’s mean and count=QUERY(DATA!A1:M13584,"select C, avg(J), count(J) where C is not null group by C",1)df.groupby("grade")["physically_active_7d"].mean(), or .count()L22
Rows before and after a fix=COUNTA(RAW!A2:A) and =COUNTA(CLEAN!C2:C), on the LOG tab, then frozenlen(df)L22

A query is a question written for a table. In the QUERY above, C and J are the columns, and group by C makes one row for each value in column C.

Chance

What you wantBy handIn SheetsFrom
Theoretical chanceThe ways it can happen ÷ all the equally likely ways.L20
Two or three in a rowMultiply the chances: 12 × 12 × 12 = 18.L20
Observed shareThe times it happened ÷ the tries.=AVERAGE(A1:A100) of a column of 1s and 0sL20
Flip a coin=RANDBETWEEN(0,1): 1 is heads, 0 is tailsL20
Longest streakCount along: the same as the flip before, add 1; different, start again at 1.=IF(A2=A1,C1+1,1) down a column, then =MAX of that columnL20
Keep a random resultCopy it, then Edit > Paste special > Values onlyL20

The next flip is always 1 in 2, whatever came before: a coin has no memory.

In pandas, coin.sample(1000, replace=True) draws 1,000 flips from a column of 0 and 1, putting each one back (L20).

The Bell

What you wantBy handIn SheetsFrom
Theoretical mean of headsflips × chance of headsL21
Theoretical SD of heads√flips × chance of heads × chance of tailsL21
z-scorez = value − meanSD=ARRAYFORMULA((K1:K1000-Q1)/Q2): a z-score for every value in K1 to K1000, with the mean in Q1 and the SD in Q2L21
On a normal curve, within1 SD2 SD3 SD
Share of the valuesabout 68%about 95%about 99.7%
z-score−2−1012
About this percent at or below (the percentile)2.5%16%50%84%97.5%

Samples

What you wantBy handIn SheetsIn pandasFrom
A value by its idFind the id, then read across to its value.=XLOOKUP(D2,RECT!A:A,RECT!B:B): find D2 in column A of RECT, give back what is beside it in column BL24
A random sample of ids=RANDBETWEEN(1,100) in each cell, then Edit > Paste special > Values onlyL20, L24
A random sample of rows=INDEX(DATA!G:G,RANDBETWEEN(2,13584)) in each cell, then frozendf["weight"].sample(50)L24
BiasSample mean − true mean. Above 0: the sample ran high.=E8-E9, with the sample’s mean in E8 and the true mean in E9L24
A simple random sample of rowsShuffle the list; take the first 10.=SORT(SCHOOL!A2:D101,RANDARRAY(100,1),TRUE), frozen; its first 10 rowsdf["sleep"].sample(10)L25
A systematic sampleStep = list size ÷ sample size. A random start from 1 to the step, then every step-th.=FILTER(SCHOOL!A2:D101,MOD(SCHOOL!A2:A101-K1,10)=0), the start in K1, frozenL25
A stratified sampleEach group’s share = group size ÷ population × sample size.=ARRAY_CONSTRAIN(FILTER(A2:D101,B2:B101=9),3,4) on a frozen shuffle: the first 3 of grade 9L25
A cluster samplePick whole groups at random; take everyone in them.=FILTER(SCHOOL!A2:D101,SCHOOL!C2:C101=U1), the homeroom in U1, frozenL25
300 random rows of your CLEAN, for SAMPLE=ARRAY_CONSTRAIN(SORT(FILTER(CLEAN!A2:B,ISNUMBER(CLEAN!B2:B)),RANDARRAY(COUNT(CLEAN!B2:B),1),TRUE),300,2), then frozenL25
How many different values=COUNTUNIQUE(RAW!A2:A): on ABOUT, what your file holds, so what it misses.nunique()L16, L24

A bigger sample picked the same way leans the same way. Only the way it is picked fixes a selection bias (L24). Let chance pick: a quota sample, where a person chooses who fills each count, lets the chooser’s lean in (L25).

How Much a Sample Wobbles

What you wantBy handIn SheetsIn pandasFrom
One bean, scooped without looking=INDEX(bowl,RANDBETWEEN(1,1000)), with bowl a named range (Data > Named ranges): the bowl’s nth bean for a random n, put backbowl.sample(25) takes 25 at onceL26
A scoop’s share redRed ÷ beans in the scoop.=AVERAGE(A1:A25) of a column of 1s and 0sscoop.mean()L20, L26
A row turned into a column=TRANSPOSE(A27:T27)L26
How much a share wobbles: its standard errorThe SD of many samples’ shares.=STDEV(V2:V21) on 20 sharesshares.std()L26
1,000 scoops at oncenp.random.binomial(n, bowl.mean(), 1000) / n, after import numpy as npL26
The square root ruleStandard error = SD ÷ √n. k times the sample divides the wobble by √k: 4 times, half; 9 times, a third.L26

A small group’s share or mean wobbles more, so small groups land at both ends of a ranking, the top and the bottom, by chance (L26).

Reading a Line of Python

  1. Read top to bottom, one line at a time.
  2. = means “call it”: the name on the left, what it is on the right.
  3. A name is a label someone made up.
  4. The dot reaches inside something.
  5. ( ) holds what you hand over. Quotes hold text, exactly as typed.
  6. print( ) shows it under the cell.

pandas prints a mean or a median as a decimal: a whole one shows .0, so 18 prints as 18.0.

Type and run your own lines on the Python practice page.