Edit

Share via


Use ai.classify with pandas

The ai.classify function uses generative AI to categorize input text according to custom labels you choose, with a single line of code.

Note

Overview

The ai.classify function extends the pandas Series class. To assign user-provided labels to each input row, call the function on a text column of a pandas DataFrame.

The function returns a pandas Series that contains classification labels, which can be stored in a new DataFrame column.

Tip

We recommend using the ai.classify function with at least two input labels.

Syntax

df["classification"] = df["input"].ai.classify("category1", "category2", "category3")

Parameters

Name Description
labels
Required
One or more strings that represent the set of classification labels to match to input text values.

Returns

The function returns a pandas Series that contains a classification label for each input text row. If a text value can't be classified, the corresponding label is null.

Example

# This code uses AI. Always review output for mistakes.

df = pd.DataFrame([
        "This duvet, lovingly hand-crafted from all-natural fabric, is perfect for a good night's sleep.",
        "Tired of friends judging your baking? With these handy-dandy measuring cups, you'll create culinary delights.",
        "Enjoy this *BRAND NEW CAR!* A compact SUV perfect for the professional commuter!"
    ], columns=["descriptions"])

df["category"] = df['descriptions'].ai.classify("kitchen", "bedroom", "garage", "other")
display(df)

This example code cell provides the following output:

Screenshot of a data frame with 'descriptions' and 'category' columns. The 'category' column lists each description’s category name.