IconResources
Conduct analysis

If then logic

Add logic to your analysis by creating new columns based on conditions.

Example Data

Follow along with right out of the box example data. Copy following data in the information request of the agent you are working in.



We often need to label rows based on rules. Is this a large transaction? Domestic or cross-border? Services or goods? This kind of conditional logic is the foundation of categorization, risk assessment, and decision-making.

In this section, you'll learn how to apply "if... then..." rules using Python — just like you would in Excel's IF() function.

Add a simple condition column

Excel

In Excel, you use =IF(A2>10000000, "Large", "Small") to label rows based on a condition.

t0 Prompt

Label each transaction as "Large" or "Small"

Add a column that says "Yes" if over 10 million

Code

The python code looks as follows:

transactions["Amount"] = pd.to_numeric(transactions["Amount"])
transactions["Size"] = np.where(transactions["Amount"] > 10_000_000, "Large", "Small")
transactions

Add multiple conditional outcomes

Excel

In Excel, you might nest IF() statements: =IF(A2>10M,"Large",IF(A2>5M,"Medium","Small")).

t0 Prompt

Label transactions as large, medium, or small

Add tiers based on amount

Use multiple thresholds in column Amount: X, Y, and Z

Code

The python code looks as follows:

transactions["Amount"] = pd.to_numeric(transactions["Amount"])
def classify(x):
    if x > 10_000_000:
        return "Large"
    elif x > 5_000_000:
        return "Medium"
    else:
        return "Small"
transactions["Category"] = transactions["Amount"].apply(classify)
transactions

Add logic based on multiple columns

Excel

In Excel, you'd write something like =IF(AND(A2="Italy",B2="Brazil"),"Cross-border","Domestic").

t0 Prompt

Flag rows where seller and buyer are in different countries

Add column for "Cross-border" or "Domestic"

Code

The python code looks as follows:

transactions["Border"] = np.where(
    transactions["Seller Country"] != transactions["Buyer Country"],
    "Cross-border",
    "Domestic"
)
transactions
FunctionDescription
np.where(condition, A, B)If condition is True, return A; otherwise return B
df["col"].apply(custom_function)Apply complex logic using a function
Combine columns with !=, ==, &, |Logic with multiple fields

On this page