<!--
{
  "availability" : [
    "iOS: 15.0.0 -",
    "iPadOS: 15.0.0 -",
    "macCatalyst: 15.0.0 -",
    "macOS: 10.14.0 -",
    "tvOS: 16.0.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "CreateML",
  "identifier" : "/documentation/CreateML/MLDataTable/subscript(_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Subscript",
  "symbol" : {
    "kind" : "Instance Subscript",
    "modules" : [
      "Create ML"
    ],
    "preciseIdentifier" : "s:8CreateML11MLDataTableVyAA15MLUntypedColumnVSScip::OverloadGroup"
  },
  "title" : "subscript(_:)"
}
-->

# subscript(_:)

Retrieves or adds an untyped column with the specified name.

```
subscript(columnName: String) -> MLUntypedColumn { get set }
```

## Parameters

`columnName`

The name of the column to extract from or add to the datatable.

## Return Value

A new [`MLUntypedColumn`](/documentation/CreateML/MLUntypedColumn) with the specified name, if it
exists; otherwise an invalid column.

## Overview

Use this subscript to retrieve an [`MLUntypedColumn`](/documentation/CreateML/MLUntypedColumn) or add a new one
to the data table.

> Important: The number of elements in the column must equal the number
> of rows in the data table. Otherwise, the data table will be
> invalidated.

![A table on the left with two columns plus a third semi-opaque column.](images/com.apple.createml/MLDataTable-subscript(_:)-3wjk-1@2x.png)

For example, to extract, convert, and add a column as shown above, begin
by creating a data table.

```swift
let data: [String: MLDataValueConvertible] = [
    "Planet": ["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"],
    "Distance (AU)": [0.39, 0.72, 1.00, 1.52, 5.20, 9.54, 19.22, 30.06]
]

var table = try MLDataTable(dictionary: data)
```

After creating the table, extract a column.

```swift
let distanceInAU = table["Distance (AU)"]
```

> Note: Without a type annotation, the compiler uses this version of
> `subscript(_:)` instead of the equivalent
> ``doc://com.apple.createml/documentation/CreateML/MLDataTable/subscript(_:)-5tl9r`` from ``doc://com.apple.createml/documentation/CreateML/MLDataColumn``.

Use the untyped column’s multiplication operator to create a new column
of data.

```swift
// Create a new column of Doubles by multiplying all the the values in
// `distanceInAU` by 149,597,870.7 (the number of km in an astronomical unit)
let distanceInKm = (distanceInAU * 149_597_870.700)
```

Finally, use the `subscript(_:)` to add the new column to a table with a
different name.

```swift
table["Distance (km)"] = distanceInKm
```

> Important: To replace a column, use
> ``doc://com.apple.createml/documentation/CreateML/MLDataTable/removeColumn(named:)`` before adding its replacement.
> Adding a column with the same name as one already in the data table may
> invalidate the data table.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)