Skip to main content

Getting Started with Python Expressions

VisiData replaces Excel formulas with Python expressions. Instead of =A2+B2, you write = amount + tax. Column names become variables. The = key creates a new derived column — the equivalent of Excel's formula bar.

Learning Focus

Everything starts with = (derived column). Learn how column names map to variables, how to handle column names with spaces, and how to view your expression results. No Python programming background is required — these are one-liners.

Who This Section Is For

This section is designed for accountants, analysts, and spreadsheet users moving from Excel or Google Sheets to VisiData. Each topic shows the Excel formula alongside its VisiData equivalent.

Python Operators Quick Reference

These operators work inside any VisiData expression (=, z|, setcol-expr, etc.):

SymbolMeaningExampleResult
=Start an expression (creates new column)= salary * 12New column with annual salary
+Add or concatenate= price + taxSum of two columns
-Subtract or negate= revenue - costDifference
*Multiply= qty * priceProduct
/Divide= total / countQuotient
**Power= side ** 2Area (side squared)
%Modulo (remainder)= id % 10Last digit of id
==Equal (comparison)= status == 'done'True if status is done
!=Not equal= region != 'US'True if not US
>Greater than= amount > 100True if over 100
<Less than= score < 50True if under 50
>=Greater or equal= age >= 18Adult check
<=Less or equal= qty <= 0Out of stock
andLogical AND= amount > 0 and status == 'active'Both must be true
orLogical OR= status == 'urgent' or priority == 1Either can be true
notLogical NOT= not approvedTrue if false
if / elseTernary conditional= 'high' if amount > 500 else 'low'Conditional value
inMembership= city in ('NYC', 'LA', 'CHI')True if one of the listed
//Integer division= total // 100Divide and drop remainder
()Grouping= (a + b) * cOverride precedence

Use with any column name as a variable:

= amount + tax # column names as variables
= 'high' if amount > 500 else 'low'
= status == 'done' or status == 'shipped'
= amount > 0 and approved == true

Excel vs VisiData — Core Concept

ExcelVisiDataNotes
=A2= column_nameReference a column by its name
=$A$1= fixed_valueConstants defined in .visidatarc
=A2 & " " & B2= first + ' ' + lastString concatenation
#REF!#ERRError when expression fails
Drag fill downi (incremental)Sequential values
Formula bar= keyOpens expression input
Name Boxstatus barShows current column name
UUndo

Opening a File

vd ~/github/practice-folder/visidata/01-loading/01-employees.csv

This opens a sheet where:

  • Each row = one employee record
  • Each column header = variable name usable in expressions
  • Column names are case-sensitive

Creating a Derived Column with =

Press = and type a Python expression. Column names are available as variables:

vd ~/github/practice-folder/visidata/01-loading/01-employees.csv

# Excel: =salary * 12
# VisiData:
=
# Enter: salary * 12
# Prompts for column name: annual_salary

# Excel: =first_name & " " & last_name
# VisiData:
=
# Enter: first_name + ' ' + last_name
# Prompts for column name: full_name

# Excel: =IF(salary>70000, "Senior", "Junior")
# VisiData:
=
# Enter: 'Senior' if salary > 70000 else 'Junior'
# Prompts for column name: tier

Column Names with Spaces

If a column name contains spaces, use row['column name']:

# Excel column header: "First Name"
# VisiData:
= row['First Name'] + ' ' + row['Last Name']

# Or: rename the column first (^ key) to remove spaces

Viewing Expression Results

The derived column appears immediately to the right of the cursor column. It is computed on demand — it does not modify the source file.

# Verify the expression worked:
# 1. Look at the new column — does it show expected values?
# 2. Check the status bar for any #ERR cells
# 3. Press Ctrl+G to see column info

Column Types Matter

Before using columns in math expressions, cast them to numeric types:

vd ~/github/practice-folder/visidata/01-loading/01-employees.csv

# Cast 'salary' to float (like telling Excel the cell is a number)
# Move to 'salary' column → press %
%

# Cast 'start_date' to date
# Move to 'start_date' column → press @
@

# Cast 'department_id' to integer
# Move to 'department_id' column → press #
#

Type casting equivalents:

ExcelVisiData KeyType
Format as Number#int
Format as Decimal%float
Format as Currency$currency
Format as Date@date
Format as Text~str

Setting Constants in .visidatarc

In Excel, you use a fixed cell reference like $B$1 for constants. In VisiData, define them in ~/.visidatarc:

# ~/.visidatarc
TAX_RATE = 0.07
GST_RATE = 0.09
FISCAL_YEAR = 2025

# Then use in expressions:
# = salary * TAX_RATE
# = amount * (1 + GST_RATE)

Bulk Set with g=

g= sets the current column for all selected rows to an expression result — like filling a formula down in Excel for selected rows only:

# Excel: select cells → fill down with Ctrl+D
# VisiData:
gs # select all rows
g=
# Enter: 'APAC'
# Sets the current column to 'APAC' for every selected row

Error Handling

ExcelVisiData
#REF!#ERR
=IFERROR(A/B, 0)= (a / b) if b != 0 else 0
#DIV/0!#ERR when dividing by zero
# Safe division:
=
# Enter: (revenue / units) if units != 0 else 0

Hands-On Practice

vd ~/github/practice-folder/visidata/01-loading/01-employees.csv

# 1. Cast salary to float: move to 'salary' → press %

# 2. Create annual salary:
=
# Enter: salary * 12
# Name: annual_salary

# 3. Create salary tier:
=
# Enter: 'high' if salary > 70000 else 'mid' if salary > 50000 else 'low'
# Name: tier

# 4. Create full name (if first_name and last_name columns exist):
=
# Enter: first_name + ' ' + last_name
# Name: full_name

# 5. Verify: scroll right to see new columns
# 6. Press Ctrl+S → save as: /tmp/employees_enriched.csv

What's Next