I have a Dataset
that requires some cleaning. Let's consider this data:
data = Dataset[{<|"ID" -> "1", "Name" -> "Bob",
"Hobbies" -> "cycling, soccer, cooking"|>, <|"ID" -> "2",
"Name" -> "Bill", "Hobbies" -> "television, gaming, tech"|>, <|
"ID" -> "3", "Name" -> "Bob",
"Hobbies" -> "reading, writing, juggling"|>}]
Most of it is relatively easy such as turning strings into integers. However, some of columns require multiple functions that may use multiple arguments. If I want to change the ID field into an integer, I can simply write this:
reviseddata = data[All, {"ID" -> ToExpression}]
However, this won't work when I need to use multiple arguments for a function such as StringTrim
.
I originally tried this, but I get a failure:
reviseddata =
data[All, {"Hobbies" -> StringTrim[StringSplit[#, ","]] &}]
This code will perform the functions correctly, but I ended up with a list of rules instead of the full dataset:
reviseddata =
data[All, {"Hobbies" -> StringTrim[StringSplit[#"Hobbies", ","]] &}]
I tried following the advice in this thread, but still ran into issues.
I know I can pull the column, do the functions on all elements, then replace the column, but I was hoping to work within this same syntax for simplicity since I am actually dealing with several similarly formatted datasets.
I am sure it is something obvious that I have missed. Thanks in advance.
reviseddata = data[All, <|#, {"ID" -> ToString[#"ID"], "Hobbies" -> StringTrim[StringSplit[#"Hobbies", ","]]}|> &]
$\endgroup$