apply_rules()
Categorize transactions with keyword rules.
Usage
apply_rules(
df,
rules,
)Each row of rules holds a regular expression (Pattern) and the Category it assigns. Rules are tried in order and the first match wins; matching ignores case. Descriptions that match no rule get "Other". From the Tracking Spending chapter.
Parameters
df: pl.DataFrame-
Transactions with a string
Descriptioncolumn. rules: pl.DataFrame-
Rules with string
PatternandCategorycolumns.
Returns
polars.DataFrame-
dfwith aCategorycolumn added (or replaced).
Raises
TypeError-
If
dforrulesis not a polars DataFrame. ValueError- If a required column is missing.
Examples
>>> import polars as pl
>>> from perfinpy import apply_rules
>>> transactions = pl.DataFrame(
... {"Description": ["SAFEWAY #123", "NETFLIX.COM", "ATM FEE"]}
... )
>>> rules = pl.DataFrame(
... {"Pattern": ["safeway", "netflix"],
... "Category": ["Groceries", "Subscriptions"]}
... )
>>> apply_rules(transactions, rules)["Category"].to_list()[‘Groceries’, ‘Subscriptions’, ‘Other’]