# Chalk DataFrames Reference: Column Operations
source: https://docs.chalk.ai/dataframe-docs#section-column-operations

Methods for selecting, transforming, and manipulating columns.

Column expressions accept `_` (underscore) syntax or functions — for example `_.price * _.qty` or `F.coalesce(_.value, 0)`. See the [Python function reference](https://docs.chalk.ai/api-docs#coalesce) for available functions.

- `select` / `drop` — pick or remove columns by name
- `with_columns` — add or replace columns while keeping existing ones
- `project` — replace _all_ columns with a new set of expressions
- `col` / `column` — reference a column by runtime name string
- `rename` — rename one or more columns
- `explode` — expand a list column into one row per element
- `with_unique_id` — append a monotonically increasing ID column

## `col(column)`
source: https://docs.chalk.ai/dataframe-docs#DataFrame.col

Return a column expression for the named column.

`df.col("name")` is equivalent to `_.name` but validates that `"name"` exists in the DataFrame's schema at call time and is therefore useful when the column name is a runtime string variable rather than a literal attribute access.

**Parameters**

- `column`: `str`
  Name of an existing column in this DataFrame.

**Returns**

`Underscore`

**Raises**

- `ValueError`: If `column` is not present in the DataFrame's schema.

**Examples**

```python
from chalkdf import DataFrame
df = DataFrame.from_dict({"x": [1, 2, 3], "y": [4, 5, 6]})
# Reference a column by name to build expressions
col_x = df.col("x")
df_filtered = df.filter(col_x > 1)
# Useful when the column name comes from a variable
target = "y"
df2 = df.with_columns({"doubled": df.col(target) * 2})
```

## `column(column)`
source: https://docs.chalk.ai/dataframe-docs#DataFrame.column

Return a column expression for the named column.

Alias for `col`.

**Parameters**

- `column`: `str`
  Name of an existing column in this DataFrame.

**Returns**

`Underscore`

**Raises**

- `ValueError`: If `column` is not present in the DataFrame's schema.

**Examples**

```python
from chalkdf import DataFrame
df = DataFrame.from_dict({"x": [1, 2, 3], "y": [4, 5, 6]})
# Compute a sum from two columns referenced by name
df2 = df.with_columns({"sum": df.col("x") + df.col("y")})
```

## `select(columns, strict)`
source: https://docs.chalk.ai/dataframe-docs#DataFrame.select

Select existing columns by name.

**Parameters**

- `columns`: `str | Underscore` = `()`
- `strict`: `bool` = `True`
  If True, raise an error if any column doesn't exist. If False, silently ignore missing columns.

**Returns**

`DataFrame`

**Examples**

```python
from chalkdf import DataFrame
df = DataFrame.from_dict({"x": [1, 2, 3], "y": [4, 5, 6], "z": [7, 8, 9]})
selected = df.select("x", "y")
```

## `drop(columns, strict)`
source: https://docs.chalk.ai/dataframe-docs#DataFrame.drop

Drop specified columns from the DataFrame.

**Parameters**

- `columns`: `str | Underscore` = `()`
- `strict`: `bool` = `True`
  If [`True`](https://docs.python.org/3/library/constants.html#True) (default), raise a [`ValueError`](https://docs.python.org/3/library/exceptions.html#ValueError) if any named column does not exist. If [`False`](https://docs.python.org/3/library/constants.html#False), silently skip missing columns.

**Returns**

`DataFrame`

**Examples**

```python
from chalkdf import DataFrame
df = DataFrame.from_dict({"x": [1, 2, 3], "y": [4, 5, 6], "z": [7, 8, 9]})
df_dropped = df.drop("z")           # raises if "z" missing
df_dropped = df.drop("z", "missing_col", strict=False)  # silently skips
```

## `with_columns(columns)`
source: https://docs.chalk.ai/dataframe-docs#DataFrame.with_columns

Add or replace columns while keeping all existing columns.

Unlike `project`, which returns _only_ the columns you specify, `with_columns` keeps every existing column and either adds new ones or replaces columns whose names match.

Accepts multiple forms:

- A [`dict`](https://docs.python.org/3/library/stdtypes.html#dict) mapping column names to expressions
- Positional tuples of `(name, expression)`
- Bare positional expressions that carry a name via `.alias(<name>)`

**Returns**

`DataFrame`

**Examples**

```python
from chalkdf import DataFrame
from chalk.features import _
import chalk.functions as F
df = DataFrame.from_dict({"x": [1, 2, 3], "y": [4, 5, 6]})
# Add a new column using underscore syntax
df2 = df.with_columns({"z": _.x + _.y})
# Add a column using an F function
df3 = df.with_columns({"z_capped": F.least(_.x + _.y, 8)})
# Add a column using .alias()
df4 = df.with_columns((_.x * 2).alias("x_doubled"))
# Both df2, df3, df4 still contain x and y in addition to the new column
```

## `project(columns)`
source: https://docs.chalk.ai/dataframe-docs#DataFrame.project

Project to an _exact_ set of output columns using expressions.

Unlike `with_columns`, which keeps all existing columns and only adds or replaces the ones you name, `project` returns _only_ the columns you specify. Columns not listed in `columns` are dropped.

Use `project` when you want to reshape or rename the schema entirely; use `with_columns` when you only want to augment it.

**Parameters**

- `columns`: `Mapping[str, Expr | Underscore | Any]`
  Mapping of output column names to expressions that define them. Every key becomes a column in the output; every value is an expression (`_` expression or `Expr`) evaluated against the current schema.

**Returns**

`DataFrame`

**Examples**

```python
from chalkdf import DataFrame
from chalk.features import _
df = DataFrame.from_dict({"x": [1, 2, 3], "y": [4, 5, 6], "z": [7, 8, 9]})
# Keep only "sum" and "x"; z is dropped
projected = df.project({"sum": _.x + _.y, "x": _.x})
```

## `rename(new_names)`
source: https://docs.chalk.ai/dataframe-docs#DataFrame.rename

Rename columns in the DataFrame.

**Parameters**

- `new_names`: `Mapping[str | Underscore, str] | Mapping[str, str]`
  Dictionary mapping old column names to new column names. Both keys and values can be either strings or underscore column references (e.g., _.col_name).

**Returns**

`DataFrame`

**Examples**

```python
from chalkdf import DataFrame
df = DataFrame.from_dict({"x": [1, 2, 3], "y": [4, 5, 6]})
renamed = df.rename({"x": "id", "y": "value"})
# Can also use underscore syntax for keys
renamed = df.rename({_.x: "id", _.y: "value"})
```

## `with_unique_id(name)`
source: https://docs.chalk.ai/dataframe-docs#DataFrame.with_unique_id

Add a monotonically increasing unique identifier column.

**Parameters**

- `name`: `str`
  Name of the new ID column.

**Returns**

`DataFrame`

**Examples**

```python
from chalkdf import DataFrame
df = DataFrame.from_dict({"x": [10, 20, 30]})
df_with_id = df.with_unique_id("row_id")
```

## `explode(column)`
source: https://docs.chalk.ai/dataframe-docs#DataFrame.explode

Explode a list or array column into multiple rows.

Each element in the list becomes a separate row, with other column values duplicated.

**Parameters**

- `column`: `str | Underscore`
  Name of the list/array column to explode.

**Returns**

`DataFrame`

**Examples**

```python
from chalkdf import DataFrame
df = DataFrame.from_dict({"id": [1, 2], "items": [[10, 20], [30]]})
exploded = df.explode("items")
```
