Sub topic 4
Loading data and leaving the raw copy alone
This chapter assumes you know basic pandas indexing, have run cells in Jupyter, and have read the setup cell chapter. It covers how to load a file into a notebook and keep the original data safe from your own edits.
Why the raw copy matters
Every cleaning rule you write is a decision. Some decisions turn out to be wrong, and you only find out later. If you overwrote the loaded data, you can no longer test a different rule. You would have to reload the file and work out which of your edits to repeat.
The fix is a naming habit. Load the data once into df_raw and never change it. All later work happens on copies that have different names.
Loading the file
The setup cell already defined RAW_PATH. Use that variable instead of typing the path again, so a path change happens in one place.
df_raw = pd.read_csv(RAW_PATH, parse_dates=["order_date"])
print(df_raw.shape)
df_raw.info()
df_raw.describe(include="all").TThree details in this cell matter:
parse_datesconvertsorder_dateto a datetime column while reading. Without it, pandas stores the dates as plain strings, and date comparisons and resampling fail later.shapeprints(rows, columns). Record these numbers. They are the baseline for every "rows kept" check after cleaning.info()lists each column's type and non-null count, anddescribe(include="all").Tgives one summary row per column.
If read_csv raises an error or a column has the wrong type, fix it here with arguments such as dtype, na_values, or usecols. Those arguments describe how to read the file. They do not clean the data.
What "leave it alone" means in practice
Assigning a DataFrame to a new name does not copy it. Both names point to the same object, so a change through one name shows up in the other.
df = df_raw # same object, not a copy
df["amount"] = 0 # this also changes df_rawTwo habits prevent this:
- Start each cleaning step with
.copy()or a method that returns a new DataFrame, such asdrop_duplicates(). - Never assign to a column of
df_raw, and never call methods withinplace=Trueon it.
You can check that the raw copy is unchanged. This assertion goes in the cell after your cleaning step:
assert df_raw.shape == (len(df_raw), df_raw.shape[1])
print(df_raw["amount"].isna().sum(), "missing amounts still present in df_raw")The second line is the useful one. If the cleaning step dropped rows with missing amounts, df_raw should still report them. If it reports zero, something modified the raw data.
Common errors
| Symptom | Likely cause |
|---|---|
| `SettingWithCopyWarning` | You modified a slice of another DataFrame. Call `.copy()` after filtering. |
| Raw data changes after cleaning | You assigned `df = df_raw` without copying. |
| Date columns are `object` type | You left out `parse_dates`, or some values do not parse. |
| Results differ after a restart | A cell depended on a variable that an earlier edit had changed. |
Keeping the file itself safe
The same rule applies on disk. Keep the original export in data/raw/ and write any cleaned output to a different folder, such as data/processed/. Treat the raw folder as read-only. Your notebook reads from it and never writes to it.
Exercise
A colleague writes df = df_raw and then runs df["region"] = df["region"].str.lower(). After that, df_raw["region"] is also lowercase. Why did this happen, and how do you change the first line to prevent it?
Show answer
The assignment df = df_raw does not copy the data. Both names refer to the same DataFrame object, so the column change affects both. Write df = df_raw.copy() so df is an independent copy, or put the change inside a function that returns a new DataFrame.