<!--
{
  "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/map(_:)-92wrj",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Create ML"
    ],
    "preciseIdentifier" : "s:8CreateML11MLDataTableV3mapyAA0C6ColumnVyxGxAC3RowVcAA0C16ValueConvertibleRzlF"
  },
  "title" : "map(_:)"
}
-->

# map(_:)

Creates a new column by applying a given thread-safe transform to every
row in the data table.

```
func map<T>(_ lazyTransform: @escaping (MLDataTable.Row) -> T) -> MLDataColumn<T> where T : MLDataValueConvertible
```

## Parameters

`lazyTransform`

A thread-safe row transformation function.

    The implementation of your transform must accept a row from the data table and return
    a type that conforms to `MLDataValueConvertible`    .

## Return Value

A new `MLDataColumn`.

## Discussion

Use this method to create a new column derived from the existing data in
the table. The closure you pass evaluates lazily only when the
transformed values are needed for a subsequent operation. Your
implementation should accept a data table row and must be thread-safe
because the framework may invoke the closure concurrently on unspecified
threads.

![A table on the left with “Day” and “Temperature” columns. The first](images/com.apple.createml/MLDataTable-map(_:)-92wrj-1@2x.png)

For example, to perform the column derivation operation shown above,
begin by creating a table of data.

```swift
let data: [String: MLDataValueConvertible] = [
    "Day": ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"],
    "Temperature": [91.3, 85.8, 79.5, 83.4, 72.2]
]

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

After creating the table, use `MLDataTable/map(_:)-92wrj` to create a
new column of data from the original data. The example closure
implementation below is stateless and safe to invoke concurrently on any
thread, so no synchronization is necessary.

```swift
let derivedColumn = table.map { row -> String in
    guard let day = row["Day"]?.stringValue,
          let temperature = row["Temperature"]?.doubleValue else {
            fatalError("Missing or invalid columns in row.")
    }
    return String(format: "%@ %.1fº", day, temperature)
}
```

Then use `MLDataTable/addColumn(_:named:)-kkbw` to add the derived
column to a table.

```swift
table.addColumn(derivedColumn, named: "Description")
```

### Mapping elements

- <doc://com.apple.documentation/documentation/swift/preventing-timing-problems-when-using-closures>
- `map(_:)-3yamp`

---

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)