Sub topic 2
Checking what was loaded
This chapter assumes you can run Python scripts or notebooks, have pandas installed, and know that a DataFrame is a table of rows and labeled columns. You should also have read the previous chapter, where read_csv first turned text into a DataFrame.
A load can succeed without error and still give you the wrong table. A column may be read as text, a header may be taken as data, or a leading zero may be gone. Checking the result right after loading catches these problems early, before they spread into later steps.
Look at the first rows
head() returns the first five rows by default. Pass a number to see more.
import pandas as pd
from io import StringIO
text = "id,name,signup\n1,Ana,2024-01-05\n2,Ben,2024-02-11\n"
df = pd.read_csv(StringIO(text))
print(df.head())
df.info()Check that the column names are what you expect. If the first row of data appears as column names, the file has no header row. If you see columns named Unnamed: 0, the file probably has an extra index column.
Read the info summary
df.info() prints a summary and returns None, so you do not wrap it in print. For the example above it shows three columns with their dtypes.
| Part of the output | What it tells you |
|---|---|
| `RangeIndex: 2 entries` | The number of rows |
| `Non-Null Count` | How many values are present in each column |
| `Dtype` | The type pandas chose for each column |
| `memory usage` | Approximate size of the DataFrame |
Compare Non-Null Count with the row count. A column with fewer non-null values than rows has missing data.
Understand the dtypes
A dtype is the data type of a column, such as int64 for whole numbers, float64 for decimals, or object for text. You can also read all of them at once with df.dtypes.
print(df.dtypes)
print(df.shape)
print(df.columns.tolist())In the example, id is int64. name and signup are text. The signup column holds dates, but pandas has read it as plain text, because read_csv does not parse dates unless you ask it to. Later chapters cover how to fix this.
df.shape gives a tuple of (rows, columns). Comparing it with the file you expect is a quick sanity check.
Common warning signs
- A numeric column shows
object. One bad value, such asN/Aor a currency symbol, can make pandas read the whole column as text. - A column that should be integers shows
float64. Missing values force this, because integer columns cannot holdNaNin the default dtype. - An ID or ZIP code column shows
int64and the leading zeros are gone. - The row count is lower or higher than the number of lines in the file. Check for blank lines, skipped bad lines, or multi-line text fields.
How the checks fit together
If a check fails, change the options in read_csv and load again. This is usually better than fixing the table afterwards, and later chapters show which options solve which problem.
Exercise
You load a file with 1,000 lines of data. df.info() shows 1000 entries, and a price column has a Non-Null Count of 940 and a dtype of float64. What does this tell you, and what would you check next?
Show answer
The column has 60 missing values (1000 minus 940). The float64 dtype is consistent with missing values appearing as NaN. Next, look at the raw file to see how those cells are written, for example empty or a marker such as N/A, and confirm they are real gaps and not values that were misread.