liwca.drop_category

On this page

liwca.drop_category#

liwca.drop_category(dx, categories)[source]#

Remove one or more categories from a dictionary.

For binary dicts, terms that no longer belong to any remaining category are dropped automatically (per dx_schema’s “drop terms with no categories” parser). For weighted dicts, all terms are preserved (a row of zeros is meaningful in a weighted dict).

Parameters:
  • dx (pandas.DataFrame) – A dictionary DataFrame. Either binary (all int8) or weighted (all float64). Mixed dtypes raise TypeError.

  • categories (str or list of str) – Category name(s) to remove.

Returns:

A new dictionary DataFrame without the specified categories, validated against the schema matching the input dtype.

Return type:

pandas.DataFrame

Raises:
  • KeyError – If any of the given category names are not present in dx.

  • TypeError – If dx has mixed dtypes (some int8, some float64).

Examples

>>> import liwca
>>> dx = liwca.create_dx({"sports": ["baseball", "hockey"], "weather": ["rain*", "snow"]})
>>> liwca.drop_category(dx, "weather")
Category  sports
DicTerm
baseball       1
hockey         1