```val df = dataFrameOf("id", "props")( 1, map...
# datascience
m
Copy code
val df = dataFrameOf("id", "props")(
    1, mapOf("a" to 10, "b" to 20),
    2, mapOf("a" to 30, "b" to 40),
)
is it possible to convert this to dataframe with columns
id, a, b
?
👀 1
j
probably the easiest way would be something like:
Copy code
df.add {
    "a" from { props["a"] }
    "b" from { props["b"] }
}.remove { props }
(if you don't have the compiler plugin or you're outside notebooks, this would look like:
Copy code
df.add {
    "a" from { "props"<Map<String, Int>>()["a"] }
    "b" from { "props"<Map<String, Int>>()["b"] }
}.remove("props")
)
(this may become easier in the future. We discovered Maps are treated differently in some places, so in the future you might be able to simply
unfold { props }
, but as of now that works unexpectedly)
a more generic solution could be:
Copy code
df.add {
    // or however you fetch all keys :)
    val keys = props.values().flatMap { it.keys }.toSet()
    
    for (key in keys) {
        key from { props[key] }
    }
}.remove { props }
I came up with another neat solution 🙂
Copy code
fun Map<String, Any?>.toDataRow() = mapValues { listOf(it.value) }.toDataFrame().single()

df.convert { props }.with { it.toDataRow() }.ungroup { props }
this
Map.toDataRow()
function will be in the 1.0 release, but not yet in Beta3
❤️ 1
m
toDataRow works perfectly. Thank you! 🙂