Don’t Duplicate Information

Contents

Don’t Duplicate Information#

Information should only be expressed once in a file. For example, say you want to drop observations from a dataset if a person’s income has ever dropped below a poverty threshold of $20,000. You could do this like this:

import pandas as pd

df = pd.DataFrame(
    {
        "income_2019": [10_000, 20_000, 30_000, 40_000, 50_000],
        "income_2018": [50_000, 40_000, 30_000, 20_000, 10_000],
        "income_2017": [50_000, 20_000, 30_000, 40_000, 50_000],
    }
)

df = df[
    (df["income_2019"] < 20_000)
    | (df["income_2018"] < 20_000)
    | (df["income_2017"] < 20_000)
]

df
income_2019 income_2018 income_2017
0 10000 50000 50000
4 50000 10000 50000

And indeed, this would work. But suppose you decided to change that cutoff from 20,000 to 30,000. The way this is written, you’ve opened yourself up to the possibility that in trying to change these cutoffs, you may change two of these but forget the third (something especially likely if the uses of the cutoff aren’t all in exactly the same place in your code). A better way of expressing this that avoids this possibility is:

df = pd.DataFrame(
    {
        "income_2019": [10_000, 20_000, 30_000, 40_000, 50_000],
        "income_2018": [50_000, 40_000, 30_000, 20_000, 10_000],
        "income_2017": [50_000, 20_000, 30_000, 40_000, 50_000],
    }
)

tax_income_threshold = 20_000

df = df[
    (df["income_2019"] < tax_income_threshold)
    | (df["income_2018"] < tax_income_threshold)
    | (df["income_2017"] < tax_income_threshold)
]

df
income_2019 income_2018 income_2017
0 10000 50000 50000
4 50000 10000 50000

Written like this, if you ever decide to go back and change the common cutoff, you only have to make in one place, and there’s no way to make the change in some cases but forget others.

No Magic Numbers#

The example above also obeys another good rule for programming: no magic numbers, meaning don’t write code that has constants (like 20_000) that appear without explanation. Assigning them to a variable — especially a well-named, self-documenting variable — first improves code legibility.