How to Force Categorical Types in Sweetviz Reports with Python

This quick tutorial shows how to override automatic type detection for categorical features in Sweetviz using the FeatureConfig force_cat parameter. For a complete introduction to automated exploratory data analysis, see our Automated Exploratory Data Analysis with Sweetviz in Python guide.

The Problem

Sweetviz automatically determines whether columns are categorical or numerical based on data types and cardinality. This usually works well, but it can fail in edge cases. Zip codes stored as integers are analyzed with means and standard deviations when they should show frequency distributions. Age groups coded as 1, 2, 3 appear in correlation matrices instead of simple category counts. Product IDs and customer codes generate meaningless statistical summaries. You need a way to tell Sweetviz that certain numeric columns represent categories, not continuous measurements.

The Solution

The FeatureConfig class provides a force_cat parameter that overrides automatic type detection. Pass a list of column names that should be treated as categorical variables regardless of their data type.

 
import sweetviz as sv
import pandas as pd

data = {'zip_code': [10001, 10002, 10001, 10003] * 5,
        'age_group': [1, 2, 3, 1, 2] * 4,
        'age': [25, 34, 45, 29, 52, 38, 41, 33, 27, 48, 31, 44, 36, 50, 28, 42, 39, 30, 46, 35],
        'amount': [45.99, 67.50, 23.00, 89.99, 34.50] * 4}
df = pd.DataFrame(data)

config = sv.FeatureConfig(force_cat=['zip_code', 'age_group'])
report = sv.analyze(df, feat_cfg=config)
report.show_html('forced_types.html')

The report now displays zip_code and age_group with frequency bar charts that show value distributions. Instead of calculating means and correlations, Sweetviz shows the mode, distinct value counts, and percentage breakdowns.

force categorical types sweetviz python 

The age column remains numerical with its histogram and complete statistics, including mean (37.6), median (37.0), and standard deviation (8.11). This separation ensures you analyze location codes and categorical groups correctly while preserving true numerical analysis for continuous variables.

A Few Tips

Use force_cat for any numeric column that represents discrete categories rather than measurable quantities. This includes customer IDs, product codes, department numbers, status flags stored as integers, and survey response codes. Binary columns stored as 0/1 often display more clearly as categorical frequencies than as numerical distributions. Note that the amount column appears categorical even though we didn’t force it. Sweetviz automatically treats columns with few distinct values (5 in this case) as categorical.

Check your data dictionary before generating reports to identify which numeric columns need categorical treatment. The force_cat parameter accepts any number of column names, so you can correct multiple misclassifications in a single configuration.

Conclusion

The force_cat parameter in FeatureConfig corrects automatic type detection for numeric columns that represent categories. Use it to ensure Sweetviz analyzes categorical codes with frequency distributions instead of meaningless statistical summaries.

Leave a Reply

Your email address will not be published. Required fields are marked *