[//]: # (title: Interop with Collections) _Kotlin DataFrame_ and _Kotlin Collection_ represent two different approaches to data storage: * [`DataFrame`](DataFrame.md) stores data by fields/columns * `Collection` stores data by records/rows Although [`DataFrame`](DataFrame.md) doesn't implement the [`Collection`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-collection/#kotlin.collections.Collection) or [`Iterable`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-iterable/) interface, it has many similar operations, such as [`filter`](filter.md), [`take`](sliceRows.md#take), [`first`](first.md), [`map`](map.md), [`groupBy`](groupBy.md) etc. [`DataFrame`](DataFrame.md) has two-way compatibility with [`Map`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-map/) and [`List`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/): * `List` -> `DataFrame`: [toDataFrame](createDataFrame.md#dataframe-from-iterable-t) * `DataFrame` -> `List`: [toList](toList.md) * `Map>` -> `DataFrame<*>`: [toDataFrame](createDataFrame.md#dataframe-from-map-string-list) * `DataFrame<*>` -> `Map>`: [toMap](toMap.md) * `List>` -> `DataFrame<*>`: [toDataFrame](createDataFrame.md#dataframe-from-list-list-t) Columns, rows, and values of [`DataFrame`](DataFrame.md) can be accessed as [`List`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/), [`Iterable`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-iterable/) and [`Sequence`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.sequences/-sequence/) accordingly: ```kotlin df.columns() // List df.rows() // Iterable df.values() // Sequence ``` ## Interop with data classes [`DataFrame`](DataFrame.md) can be used as an intermediate object for transformation from one data structure to another. Assume you have a list of instances of some [data class](https://kotlinlang.org/docs/data-classes.html) that you need to transform into some other format. ```kotlin data class Input(val a: Int, val b: Int) val list = listOf(Input(1, 2), Input(3, 4)) ``` You can convert this list into [`DataFrame`](DataFrame.md) using [`toDataFrame()`](createDataFrame.md#todataframe) extension: ```kotlin val df = list.toDataFrame() ``` Mark the original data class with [`DataSchema`](schemas.md) annotation to get [extension properties](extensionPropertiesApi.md) and perform data transformations. ```kotlin @DataSchema data class Input(val a: Int, val b: Int) val df2 = df.add("c") { a + b } ``` To enable extension properties generation, you should use the [DataFrame plugin](schemasGradle.md) for Gradle or the [Kotlin Jupyter kernel](SetupJupyter.md) After your data is transformed, [`DataFrame`](DataFrame.md) instances can be exported eagerly into [`List`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/) of another data class using [toList](toList.md) or [toListOf](toList.md#tolistof) extensions: ```kotlin data class Output(val a: Int, val b: Int, val c: Int) val result = df2.toListOf() ``` ```kotlin data class Output(val a: Int, val b: Int, val c: Int) val result = df2.toListOf() ``` Alternatively, one can create lazy [`Sequence`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-sequence/) objects. This avoids holding the entire list of objects in memory as objects are created on the fly as needed. ```kotlin val df = dataFrameOf("name", "lastName", "age")("John", "Doe", 21) .group("name", "lastName").into("fullName") data class FullName(val name: String, val lastName: String) data class Person(val fullName: FullName, val age: Int) val persons = df.toListOf() // [Person(fullName = FullName(name = "John", lastName = "Doe"), age = 21)] ``` ### Converting columns with object instances to ColumnGroup [unfold](unfold.md) can be used as [`toDataFrame()`](createDataFrame.md#todataframe) analogue for specific columns inside existing dataframes