Zum Inhalt springen

Lektion 9 · Cleaning and Transforming

Missing Values, Duplicates, and Data Types in KNIME

Handle missing values, find duplicate rows, and convert column types in KNIME with the Missing Value and Duplicate Row Filter nodes, without hiding problems.

Three problems appear in almost every real dataset: gaps, repeated records, and columns with the wrong type. Each has a node that fixes it, but the node only does what you decide. This lesson focuses on the decisions as much as the clicks.

Profile before you fix

Start by looking. The node monitor's statistics view gives you column types and basic summaries. For a wider summary, KNIME has statistics nodes that report counts, ranges, and missing values. Write down what you find before changing anything.

Python Stand-in: a tiny data profile

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Note that "n/a" is not automatically a missing value. If a source uses words like n/a, -, or 999 to mean "unknown", convert those to real missing values first.

The Missing Value node

The Missing Value node lets you choose how to treat gaps, either for all columns of a type or column by column. Typical choices include:

Ways to handle gaps

Remove rows Loses information
Fixed value Like Unknown or 0
Statistic Mean, median, mode
Neighbor Previous or next value
1 / 2

There is no universally right choice:

  • Zero is right for "no sales recorded means no sales", and wrong for "the sensor was offline".
  • The mean keeps the average the same but makes the data look less varied.
  • Previous value only makes sense for ordered data such as a time series.
  • Removing rows can bias results if the gaps are not random, for example if one region fails to report.

A useful trick is to first add a column that flags where values were missing, then fill the gaps. You keep the information that something was unknown.

Python Stand-in: flag first, then fill with the median

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

For machine learning, compute the fill value from the training data only. The machine learning basics lesson explains why.

Duplicate rows

The Duplicate Row Filter node finds rows that match on the columns you select. You choose which columns define "the same" and what to do with duplicates, such as removing them or marking them.

The column choice is the whole point:

  • All columns equal: exact copies, probably a double export.
  • Same order_id: the same order, even if a status changed.
  • Same email: perhaps the same customer, perhaps a shared family address.

When rows differ but share a key, decide which one to keep, often the newest. Sort by date first so the kept row is predictable, and check which row the node keeps in your settings.

SQL Stand-in: keep the latest record per order

Press Run (or Ctrl + Enter) to see the result here.

Converting types

When a column is the wrong type, convert it with a dedicated node, for example String to Number, Number to String, or String to Date&Time. You can also set types in a reader's Transformation tab, as shown in reading files.

Conversions can fail for individual values such as "12 kg". Check what happens to those rows: whether the node stops with an error or turns them into missing values. Clean the text first, as explained in the next lesson, and count missing values before and after converting.

Never fill gaps or drop rows just to make a node stop complaining. First find out why the values are missing. A broken export is something to fix at the source, not to hide.

Record your decisions

In a workflow annotation, write short rules such as "Blank discount means 0 (confirmed by sales team)". The next person can then tell a rule from a guess.

Why values are missing

The reason for a gap changes the right fix. It helps to sort gaps into rough groups:

  • Never applicable. A "cancellation date" is empty for active customers. This is normal information, not a defect.
  • Not collected. A form field was optional, so some people skipped it.
  • Lost. An export or join failed, so values that exist in the source did not arrive.
  • Hidden codes. The source writes 999, -1, or unknown to mean missing.

"Lost" values should be fixed at the source, and hidden codes should be turned into real missing values. A quick check is to see whether gaps cluster by date, region, or system. If every missing value comes from one file, you have probably found a pipeline problem, not a customer behavior.

Near duplicates

Exact duplicate detection misses records that differ by a space, capitalization, or a typo. Standardize the text first, as in the next lesson, and then look for duplicates on the cleaned key. Be careful when merging people: two records with the same name may be two different people. Matching on several fields is safer than matching on one, and uncertain matches should go to a human for review rather than being merged automatically.

Checking your work

After every cleaning stage, compare the following before and after: the row count, the number of missing values in key columns, the number of distinct keys, and one numeric total. If a number changes more than you expected, stop. A cleaning step that removes thousands of rows might be correct, but you need to know why and be able to say so.

Practice task

Build a table of twelve rows with two exact duplicates, one near duplicate, an n/a code, and a real blank. Clean it, and write one annotation per rule that explains your choice.

Choosing where to clean

Cleaning can happen in three places: in the source system, in a database query, or in your workflow. Fixing the source helps every user of the data, but it is often outside your control. Cleaning in the workflow is flexible and visible, but other reports will not benefit. Whatever you choose, avoid cleaning the same problem in different ways in different workflows, because two reports will then disagree. When a rule is shared, put it in one component or one query and reuse it.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. When is filling missing values with 0 most reasonable?

    Zero is correct only if the absence of a value really means zero.
  2. 2. What decides whether two rows are duplicates?

    Duplicate detection depends on the selected key columns.
  3. 3. Why add a missing-value flag before filling?

    After filling, you can no longer tell which values were real without a flag.
  4. 4. A column holds values such as 12 kg. What should you do before converting to numbers?

    Non-numeric text must be cleaned, and the conversion result checked.

Summary

  • Profile data first and turn coded unknowns into real missing values.
  • Choose a missing-value method by what the gap means.
  • Flag imputed values when the information matters.
  • Define duplicates by chosen key columns and control which row stays.
  • Check conversion results and document your rules.

Next: Cleaning Text and Dates

Dein Fortschritt wird in diesem Browser gespeichert. Kein Konto nötig.