Open cmelchior opened 1 month ago
Working solution
val df = (1..1000).toDataFrame {
"id" from { it }
"value" from {"value$it" }
}
Need to check - does it have the same behaviour in the Gradle projects
It works fine in gradle projects, so we'll need to check what weird type inference is going on in the Jupyter integration...
It creates a DataFrame like:
entries | keys | size | values |
---|---|---|---|
[id=1, value=value1] | [id, value] | 2 | [1, value1] |
[id=2, value=value2] | [id, value] | 2 | [2, value2] |
[id=3, value=value3] | [id, value] | 2 | [3, value3] |
[id=4, value=value4] | [id, value] | 2 | [4, value4] |
[id=5, value=value5] | [id, value] | 2 | [5, value5] |
[id=6, value=value6] | [id, value] | 2 | [6, value6] |
[id=7, value=value7] | [id, value] | 2 | [7, value7] |
[id=8, value=value8] | [id, value] | 2 | [8, value8] |
[id=9, value=value9] | [id, value] | 2 | [9, value9] |
[id=10, value=value10] | [id, value] | 2 | [10, value10] |
However, was this the solution you were looking for?
I suspect you want to create a dataframe with a column id
and a column value
right? Then indeed @zaleslaw 's solution works great.
Constructing a DataFrame is usually done by column and not by row, as that's how they're stored in memory. That's why all DataFrame creation methods are built the way they are. If you have a List<Map<String, T>>
and you want each map to be like a row, you could make something like this:
val df = (1..1000).map {
mapOf("id" to it, "value" to "value$it")
}.toDataFrame {
source.map { it["id"] }.toColumn() into "id"
source.map { it["value"] }.toColumn() into "value"
}
If you really have to construct a DataFrame row by row, in theory you could but it would essentially entail making many small DFs and concatenating them, like:
val df = (1..1000).map {
mapOf("id" to it, "value" to "value$it")
}.map {
dataFrameOf(header = it.keys, values = it.values)
}.concat()
Ah yeah. Good explanation. I just copied some code from ChatGPT as part of testing something else when I saw the crash. So I didn't realize that it did things slightly wrong.
This code:
Crashes Kotlin Notebooks with: