Initializing playground…
← Back to roadmap

📊 value_counts — Interactive Playground

Count unique values in a Series or unique row combinations in a DataFrame. Mirrors pandas.Series.value_counts() and pandas.DataFrame.value_counts().
Edit any code block below and press ▶ Run (or Ctrl+Enter) to execute it live in your browser.

1 · Basic usage (Series)

Create a Series and count how often each unique value appears. Results are sorted descending by frequency by default.

TypeScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent

2 · Normalize — return proportions

Pass normalize: true to get relative frequencies that sum to 1 instead of raw counts.

TypeScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent

3 · Sort order

Control sorting: ascending: true for least-frequent first, or sort: false to preserve insertion order.

TypeScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent

4 · Missing-value handling

By default nulls are excluded (dropna: true). Set dropna: false to include them in the count.

TypeScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent

5 · DataFrame value_counts

Count unique row combinations across all columns. Each unique (city, temp) pair becomes an index label.

TypeScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent

6 · DataFrame subset

Restrict counting to a subset of columns with the subset option.

TypeScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent

API Reference

The result Series is indexed by the unique values (or composite "v1|v2|…" strings for DataFrames). Use .index.values and .values to inspect the labels and counts respectively.

// Series
valueCounts(series, {
  normalize?: boolean,   // default false — return proportions
  sort?:      boolean,   // default true  — sort by frequency
  ascending?: boolean,   // default false — highest count first
  dropna?:    boolean,   // default true  — exclude missing values
}): Series<number>

// DataFrame
dataFrameValueCounts(df, {
  subset?:    readonly string[],  // columns to use (default: all)
  normalize?: boolean,
  sort?:      boolean,
  ascending?: boolean,
  dropna?:    boolean,
}): Series<number>