Similar to last question... How can I get DataFram...
# datascience
d
Similar to last question... How can I get DataFrame to understand that any value that has a % after the number (currently it makes it into a string...) should be a number where the % is removed and casting the number into an Int (preferably a
value class
that's displayed as a percentage, but can be plotted).. maybe even if I could just specify the columns that have such values?
n
We don't have enough configuration options to enable automatic parsing of such values similar to how readCSV parses something like Double. So the only option is to convert selected columns to this value class
df.convert { col1 and col2 }.with { TODO() }
More complex selector can help to convert all suitable columns
Copy code
// Just for reference, need to create an actual value class here
fun String.toPercentageOrNull() = if(endsWith("%")) dropLast(1).toIntOrNull() else null

dataFrameOf("a","b")("55%", "12%").convert { 
    colsAtAnyDepth()
        .colsOf<String>()
        .filter {
            it.values().all { it.toPercentageOrNull() != null } 
        } 
}.with { it.toPercentageOrNull()!! }
🤔 1