Sub topic 5
A first look at shape, types, and summaries
This chapter assumes you can read a CSV with pandas, know what a DataFrame and a dtype are, and have run cells in Jupyter. It covers the first cells you run after loading data. They tell you how big the data is, what each column holds, and what the numbers look like, before you clean or plot anything.
Why a first look comes first
A first look is cheap and catches problems early. A date column read as text, a numeric column read as strings, or a file with 12 rows instead of 12,000 will skew every later step. You want to see these before you write cleaning rules.
Keep the raw data untouched. Load it into df_raw and only read from it in this section.
Shape, types, and non-null counts
df_raw = pd.read_csv(RAW_PATH, parse_dates=["order_date"])
print(df_raw.shape)
df_raw.info()
df_raw.describe(include="all").TThis cell assumes the setup cell from the earlier chapter has run, so pd and RAW_PATH exist.
shapereturns(rows, columns). Compare it with what you expect from the source.info()lists each column's dtype and its non-null count. A column with fewer non-null values than rows has missing data.describe(include="all")summarizes numeric and non-numeric columns together. The.Ttransposes the result so each column of the data is a row, which is easier to read when there are many columns.
What to look for
| Signal | What it may mean |
|---|---|
| `object` dtype on a numeric-looking column | Stray text such as `"N/A"` or a currency symbol |
| `object` dtype on a date column | Dates were not parsed |
| Non-null count below the row count | Missing values to audit next |
| `min` below zero for an amount | An implausible value or a refund |
| `mean` far from `50%` (the median) | A skewed distribution or outliers |
| `unique` equal to the row count | A likely identifier column |
Treat these as leads, not conclusions. The next chapter audits them properly.
Look at actual rows
Summaries hide things that raw rows show, such as inconsistent casing or odd formats. Add a quick look at the top and a random sample:
display(df_raw.head())
display(df_raw.sample(5, random_state=SEED))head() shows how the file starts. The sample shows rows from elsewhere, and the fixed random_state makes the same rows appear on every run.
Common errors
- Using
describe()on only the numeric default. Withoutinclude="all", text and date columns are dropped, and you may not notice they exist. - Trusting dtypes blindly. A column that parses as
float64can still contain impossible values. Dtype tells you the storage type, not whether the values make sense. - Fixing things as you spot them. If you find a problem here, write it down and fix it later in the named cleaning function. Editing
df_rawin place breaks the rule that the raw copy stays untouched.
Exercise
After loading a file, info() shows 10,000 rows in shape but 9,400 non-null values in amount, and amount has dtype object. Name two things this suggests and where you should fix them.
Show answer
The column has about 600 missing values, and it contains non-numeric text (such as "N/A" or a currency symbol) that stopped pandas from reading it as numbers. Do not edit df_raw. Record both observations, audit them in the data quality section, and fix them in the cleaning function that returns a new DataFrame.