1
$\begingroup$

Take a small dataset, for example a sample of the Titanic data:

ds=Dataset@{<|"PassengerId" -> 1, "Survived" -> 0, "Pclass" -> 3, 
"Name" -> "Braund, Mr. Owen Harris", "Sex" -> "male", "Age" -> 22, 
"SibSp" -> 1, "Parch" -> 0, "Ticket" -> "A/5 21171", "Fare" -> 7.25,
 "Cabin" -> "", "Embarked" -> "S"|>, <|"PassengerId" -> 2, 
"Survived" -> 1, "Pclass" -> 1, 
"Name" -> "Cumings, Mrs. John Bradley (Florence Briggs Thayer)", 
"Sex" -> "female", "Age" -> 38, "SibSp" -> 1, "Parch" -> 0, 
"Ticket" -> "PC 17599", "Fare" -> 71.2833, "Cabin" -> "C85", 
"Embarked" -> "C"|>, <|"PassengerId" -> 3, "Survived" -> 1, 
"Pclass" -> 3, "Name" -> "Heikkinen, Miss. Laina", 
 "Sex" -> "female", "Age" -> 26, "SibSp" -> 0, "Parch" -> 0, 
"Ticket" -> "STON/O2. 3101282", "Fare" -> 7.925, "Cabin" -> "", 
"Embarked" -> "S"|>, <|"PassengerId" -> 4, "Survived" -> 1, 
"Pclass" -> 1, 
"Name" -> "Futrelle, Mrs. Jacques Heath (Lily May Peel)", 
"Sex" -> "female", "Age" -> 35, "SibSp" -> 1, "Parch" -> 0, 
"Ticket" -> 113803, "Fare" -> 53.1, "Cabin" -> "C123", 
"Embarked" -> "S"|>, <|"PassengerId" -> 5, "Survived" -> 0, 
"Pclass" -> 3, "Name" -> "Allen, Mr. William Henry", 
"Sex" -> "male", "Age" -> 35, "SibSp" -> 0, "Parch" -> 0, 
"Ticket" -> 373450, "Fare" -> 8.05, "Cabin" -> "", 
"Embarked" -> "S"|>}

I want to extract, for example, the age of a particular passenger. I can group by name:

 dsName=ds[GroupBy["Name"]]

But trying to extract the age of a given passenger with

dsName[["Heikkinen, Miss. Laina","Age"]]

results in an error message. What is the correct way of doing this?

$\endgroup$
2
$\begingroup$
dsName[["Heikkinen, Miss. Laina", All,"Age"]]

enter image description here

You can also use

dsName["Heikkinen, Miss. Laina", All,"Age"]

dsName["Heikkinen, Miss. Laina"][All,"Age"]

ds[Select[#"Name"=="Heikkinen, Miss. Laina"&], "Age"]

ds[Select[#"Name" == "Heikkinen, Miss. Laina" &]][All, "Age"]
$\endgroup$
  • 1
    $\begingroup$ Do you know why the method I tried does not work? $\endgroup$ – Whelp Oct 14 at 13:46
  • 1
    $\begingroup$ @Whelp, if you look at the target row using dsName[["Heikkinen, Miss. Laina"]]// Normal you see that it is of the form {<|key1 -> value1,...|>}(your method would have worked if this were <|key1 -> value1,...|>) ; so we need to use All (1 would have worked too) to get all elements in this added dimension. $\endgroup$ – kglr Oct 14 at 13:57
  • 1
    $\begingroup$ You can also Transpose the dataset after your GroupBy so that the levels are what you'd expect. dsName = ds[GroupBy["Name"], Transpose], and then you can do dsName["Heikkinen, Miss. Laina", "Age"] as you'd expect $\endgroup$ – Carl Lange Oct 15 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.