Cleaning data with pandas: the order for handling nulls, duplicates, wrong types and dirty text
The order that works when cleaning data with pandas is: inspect, standardize text, fix types, drop duplicates, handle nulls, and only then evaluate outliers. Each step depends on the one before it, because dirty text hides nulls, a null disguised as a string breaks type conversion, and duplicates inflate the count of missing values. The Python for Data Science and Analytics course covers data cleaning and preparation and outlier detection across its Basic, Intermediate and Advanced modules.
What is the correct order for cleaning data with pandas?
The order is not cosmetic. It is what keeps you from doing the same work twice. If you handle nulls before standardizing text, you count as missing something that was only a formatting difference. If you drop duplicates before standardizing, you let a duplicate through because the same record was written two different ways.
Start with inspection and change nothing yet. Then follow the sequence below, checking the effect of each step before moving to the next one.
- Inspection: df.shape, df.info(), df.head() and df.describe() to see types, ranges and suspicious values.
- Text standardization: strip whitespace, fix upper and lower case, remove accents, and replace markers such as N/A, - or unknown with a real missing value.
- Type conversion: to_numeric, to_datetime and astype applied only after the text is clean.
- Duplicates: df.duplicated() and drop_duplicates(), with the columns that define a record chosen explicitly.
- Nulls: df.isna().sum() per column, then a decision to drop the row, fill the value or keep it missing.
- Outliers: detection by IQR, z-score or boxplot once the frame is already clean.
Why standardize text before converting data types?
A value column can arrive as text: 1,200.00 with a currency symbol, 1.200,00 with a unit attached, 42 with trailing spaces, or UNKNOWN sitting in the middle of numbers. In that state pandas reads the whole column as object.
Call to_numeric on it directly and the conversion either fails or turns everything into NaN. That creates nulls that never existed in the source data, and every decision after that is made on top of a problem you introduced yourself.
Standardizing first separates two different things: formatting noise and genuine absence. Only the second one is a null. The same logic applies to column names with spaces, accents or parentheses, which get in the way of attribute access.
Duplicates or nulls: which comes first?
Duplicates come first, because they distort the count. Three copies of a row with a missing value count as three nulls. The share of missing data looks inflated and pushes you toward a wrong call, such as discarding a column that was perfectly usable.
There is also the false duplicate: two identical records written differently, like Sao Paulo and sao paulo, or a name with an extra space. That is why text standardization has to happen before any duplicate comparison.
When you call drop_duplicates(), state the columns that identify a record. Comparing the entire row can keep a duplicate that differs only in one field that was updated later.
How do you tell a wrong type from missing data?
df.info() answers it. A date column showing up as object, a value column with a decimal comma, or an identifier read as a number that loses leading zeros are signs of a wrong type, not of absence.
The practical test is to convert and compare. Build a converted column, count how many values became NaN, and look at which ones they were. If the original value had content, it was a type problem. If it was already empty, it was a null.
Remember that an identifier is not a number for calculation. Postal codes, student IDs and product codes usually work better as text, because nobody sums a postal code.
Where do outliers fit in this order?
Outliers are the last step, because the limits that define what counts as extreme depend on a distribution that is already clean. A mean and a standard deviation calculated with duplicates and nulls in place produce false thresholds, and what looked like an outlier may only be the effect of earlier dirt.
Once the frame is clean, detection separates two different cases: a data entry error, such as an age of 200, which is a value and type problem; and a legitimate extreme value, such as a genuinely high income, which is information rather than an error.
Whether to remove, keep or transform it depends on the goal of the analysis and of the model. The course treats outlier detection as its own stage, separate from basic cleaning.
How does the course teach this order in practice?
The course is built in three main modules, Basic, Intermediate and Advanced, plus bonus modules, and data cleaning and preparation sits inside that path instead of being a side topic. It starts with IDE and Jupyter Notebook setup, basic functions, loops and conditionals, and user-defined functions, then moves into NumPy and Pandas, data visualization, cleaning and preparation, and outlier detection.
From there the Advanced track covers machine learning: linear and logistic regression, K-Means, SVM, Naive Bayes, decision trees, Random Forest, Gradient Boosting, XGBoost, LightGBM, feature selection and cross-validation. It closes with a final project: end-to-end exploratory analysis and machine learning.
Lessons are recorded in high quality and watched at your own pace, with guided practice exercises and real market projects you can use in a portfolio. Access is unlimited with no expiry date, there is a certificate of completion and an exclusive community for networking, and no prior programming experience is required. Enrollment runs through the Hotmart checkout, by card or boleto, at R$ 470 or 12 installments of R$ 20,37 on a card, with details on python.paanalytics.net.
| Step | Typical pandas call | What goes wrong if you reorder it |
|---|---|---|
| Inspect | df.info(), df.head(), df.describe() | You edit before you know the types, and fix the wrong column |
| Standardize text | str.strip(), str.lower(), str.replace() | Nulls stay hidden inside strings and type conversion fails |
| Fix types | to_numeric(), to_datetime(), astype() | Conversion turns real values into NaN and invents missing data |
| Drop duplicates | df.duplicated(), drop_duplicates() | Missing-value counts stay inflated and decisions are made on false shares |
| Handle nulls | df.isna().sum(), fillna(), dropna() | You fill or drop values that were never actually missing |
| Evaluate outliers | IQR, z-score, boxplot | Thresholds are computed on a dirty distribution and flag the wrong rows |
Sequence and pandas methods reflect the cleaning and preparation workflow taught in the course's Pandas module; the failure modes describe the order dependency explained on this page.
Start cleaning data with pandas in the right order
Secure payment via Hotmart, up to 12 installments.
Enroll now Product siteFrequently asked questions
Do I need to know Python before learning pandas?
No. The course starts from zero and does not require prior programming experience. It begins with IDE and Jupyter Notebook setup and basic functions before reaching NumPy and Pandas.
Should I drop rows with nulls or fill them?
Decide after text standardization and duplicate removal, because those two steps change how many nulls you actually have. Then look at the column, the share of missing values and what the analysis needs. The course covers cleaning and preparation as a dedicated stage, including the choice between dropping, filling and keeping a missing value.
Can I clean data in a spreadsheet instead of pandas?
A spreadsheet is fine for a one-off look at a small file, and it is often faster for a quick manual check. A pandas script is reproducible: you can re-run the same steps on next month's file and get the same treatment, and you can version it. The two are not mutually exclusive, and the course works with pandas inside Jupyter Notebook.
How much does the course cost and how do I pay?
The course costs R$ 470, or 12 installments of R$ 20,37 on a card. Payment goes through the Hotmart checkout, by card or boleto, and the enrollment link is on python.paanalytics.net.
Does the course also cover outlier detection, machine learning and a certificate?
Yes. Outlier detection is part of the cleaning and preparation content, and the Advanced track covers linear and logistic regression, K-Means, SVM, Naive Bayes, decision trees, Random Forest, Gradient Boosting, XGBoost, LightGBM, feature selection and cross-validation. The course closes with an end-to-end final project, includes guided exercises and real market projects for a portfolio, and issues a certificate of completion. Access is unlimited with no expiry date, and there is an exclusive community for networking.