How to select multiple numerical text columns using sklearn Pipeline FeatureUnion for text classification?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Text classification pipelines often need more than just one text column. Real datasets may include title text, body text, and numerical metadata such as length, score, or count features. In scikit-learn, the core idea is to build separate transformations for each feature group and then concatenate their outputs before classification. Historically, FeatureUnion was the common tool for this. In modern scikit-learn, ColumnTransformer is often cleaner, but understanding the union pattern still helps.
Think in Terms of Parallel Feature Pipelines
Suppose a dataset has:
titleas short text.bodyas longer text.num_linksas a numeric feature.author_scoreas another numeric feature.
A good pipeline processes each group independently:
- Vectorize
titletext. - Vectorize
bodytext. - Scale or pass through numeric columns.
- Concatenate everything.
- Train the classifier.
That is exactly what FeatureUnion and ColumnTransformer are designed for.
A Practical ColumnTransformer Example
For dataframe-based workflows, ColumnTransformer is usually simpler than hand-built FeatureUnion selectors.
This is usually the most maintainable answer for multiple text and numeric columns in current scikit-learn.
Where FeatureUnion Fits In
FeatureUnion is still useful when each branch is itself a custom pipeline and you want to combine their transformed outputs explicitly.
A common older pattern is:
- Select one column.
- Apply a vectorizer or numeric transformer.
- Repeat for the next column.
- Combine with
FeatureUnion.
That requires custom column selectors because FeatureUnion itself does not know how to choose dataframe columns.
Then you can build separate branches and union them.
A FeatureUnion Example
This works, but it is more manual than the ColumnTransformer version.
Choose the Right Tool for the Codebase
A practical rule is:
- Use
ColumnTransformerwhen your inputs are structured columns and the preprocessing is column-driven. - Use
FeatureUnionwhen you already have independent transformation pipelines whose outputs should be concatenated.
The title question mentions FeatureUnion, but for many modern projects the better answer is “use ColumnTransformer unless you specifically need the union abstraction.”
Keep Sparse and Dense Features in Mind
Text vectorizers usually produce sparse matrices, while scaled numeric features may start dense. Scikit-learn can combine them, but you should still be aware of the resulting matrix format and the classifier's suitability.
Linear models such as logistic regression and linear SVMs usually work well with these mixed sparse feature spaces. Some other estimators are less convenient or less efficient in that setup.
Common Pitfalls
- Trying to feed multiple dataframe columns directly into one vectorizer without selecting them properly first.
- Using
FeatureUnionwithout a column-selection step and getting shape or type errors. - Ignoring that
ColumnTransformeris often the simpler modern solution for mixed-column preprocessing. - Combining sparse text features with dense numeric features without thinking about estimator compatibility.
- Preprocessing columns outside the pipeline and making training and inference transformations drift apart.
Summary
- Mixed text-and-numeric classification problems should be modeled as parallel preprocessing branches that are concatenated before classification.
- '
ColumnTransformeris usually the cleanest modern solution for multiple text and numeric columns.' - '
FeatureUnionstill works when you want to combine independently built transformation pipelines.' - Column selection is the essential step that makes multi-column preprocessing correct.
- Keeping preprocessing inside the scikit-learn pipeline is the best way to preserve reproducibility between training and prediction.

