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 want | In Sheets | In pandas | From |
| Rows of data after an import | =COUNTA(A:A)-1: the filled cells in column A, minus the header row | len(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 numbers | Fix "12 cm" and blanks by hand | pd.to_numeric(..., errors="coerce") | L19b |
| Drop the blanks | Filter 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 want | By hand | In Sheets | In pandas | From |
| Mean | Add, then divide by how many. | =AVERAGE(B2:B33) | .mean() | L17, L19b |
| Median | Put them in order; the middle value. Two middles: their mean. | =MEDIAN(B2:B33) | .median() | L17, L19b |
| Histogram | Count how many values fall in each range. | Click the column letter. Insert > Chart. On Setup, set Chart type to Histogram chart. | | L17 |
| Smallest, largest | The first and last in order. | =MIN(B2:B33), =MAX(B2:B33) | .max() for the largest | L18, L19b |
| Q1, Q3 | The 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 |
| IQR | Q3 − Q1. | Q3’s cell minus Q1’s | | L18 |
| SD | The six steps below. | =STDEV(B2:B33) | .std() | L19, L19b |
The SD by Hand
- Find the mean.
- Take the mean away from each value.
- Square each answer, so none is negative.
- Add the squares.
- Divide by n − 1, one less than how many values.
- 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 want | In Sheets | In pandas | From |
| Keep only some rows | Data > 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 kept | L22 |
| One group’s count | =COUNTIF(CLEAN!A:A,A7) | len( ) of the rows you kept | L22 |
| 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 frozen | len(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 want | By hand | In Sheets | From |
| Theoretical chance | The ways it can happen ÷ all the equally likely ways. | | L20 |
| Two or three in a row | Multiply the chances: 12 × 12 × 12 = 18. | | L20 |
| Observed share | The times it happened ÷ the tries. | =AVERAGE(A1:A100) of a column of 1s and 0s | L20 |
| Flip a coin | | =RANDBETWEEN(0,1): 1 is heads, 0 is tails | L20 |
| Longest streak | Count 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 column | L20 |
| Keep a random result | | Copy it, then Edit > Paste special > Values only | L20 |
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 want | By hand | In Sheets | From |
| Theoretical mean of heads | flips × chance of heads | | L21 |
| Theoretical SD of heads | √flips × chance of heads × chance of tails | | L21 |
| z-score | z = 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 Q2 | L21 |
| On a normal curve, within | 1 SD | 2 SD | 3 SD |
| Share of the values | about 68% | about 95% | about 99.7% |
| z-score | −2 | −1 | 0 | 1 | 2 |
| About this percent at or below (the percentile) | 2.5% | 16% | 50% | 84% | 97.5% |
Samples
| What you want | By hand | In Sheets | In pandas | From |
| A value by its id | Find 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 B | | L24 |
| A random sample of ids | | =RANDBETWEEN(1,100) in each cell, then Edit > Paste special > Values only | | L20, L24 |
| A random sample of rows | | =INDEX(DATA!G:G,RANDBETWEEN(2,13584)) in each cell, then frozen | df["weight"].sample(50) | L24 |
| Bias | Sample mean − true mean. Above 0: the sample ran high. | =E8-E9, with the sample’s mean in E8 and the true mean in E9 | | L24 |
| A simple random sample of rows | Shuffle the list; take the first 10. | =SORT(SCHOOL!A2:D101,RANDARRAY(100,1),TRUE), frozen; its first 10 rows | df["sleep"].sample(10) | L25 |
| A systematic sample | Step = 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, frozen | | L25 |
| A stratified sample | Each 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 9 | | L25 |
| A cluster sample | Pick whole groups at random; take everyone in them. | =FILTER(SCHOOL!A2:D101,SCHOOL!C2:C101=U1), the homeroom in U1, frozen | | L25 |
| 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 frozen | | L25 |
| 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 want | By hand | In Sheets | In pandas | From |
| 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 back | bowl.sample(25) takes 25 at once | L26 |
| A scoop’s share red | Red ÷ beans in the scoop. | =AVERAGE(A1:A25) of a column of 1s and 0s | scoop.mean() | L20, L26 |
| A row turned into a column | | =TRANSPOSE(A27:T27) | | L26 |
| How much a share wobbles: its standard error | The SD of many samples’ shares. | =STDEV(V2:V21) on 20 shares | shares.std() | L26 |
| 1,000 scoops at once | | | np.random.binomial(n, bowl.mean(), 1000) / n, after import numpy as np | L26 |
| The square root rule | Standard 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
- Read top to bottom, one line at a time.
= means “call it”: the name on the left, what it is on the right.
- A name is a label someone made up.
- The dot reaches inside something.
( ) holds what you hand over. Quotes hold text, exactly as typed.
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.