9
$\begingroup$

I created a dateset (estimates) with 25 keys (my columns), and about a hundred rows. I'd like to be able to list the keys from the dataset.

I did this:

estimates[Keys]

which returned:

enter image description here

I also tried:

estimates[1]

which returned:

enter image description here

How can I display the entire list of keys?

My code is below:

Module[
{fileTemp, header, data}, 
fileTemp = Import[NotebookDirectory[] <> "test.xlsx", {"Sheets", "estimates"}];
header = fileTemp[[1]];
data = fileTemp[[2 ;;]];
estimates = Dataset@Map[AssociationThread[header, #] &]@data;

]

The following is part of the Excel sheet:

enter image description here

$\endgroup$
4
  • $\begingroup$ I am now curious why your command estimates[Keys] worked when I get an error: "Cannot apply Keys to an expression of the form {__Association}" -- would you please check to see if e.g. titanic[Keys] works on your system, using the definition from my answer? $\endgroup$
    – Mr.Wizard
    Commented Aug 6, 2014 at 20:21
  • $\begingroup$ I also get an error with titanic[Keys]. I checked and the syntax is the same as my estimates[Keys]. Maybe my estimates dataset is improperly formatted. If I just type titanic I see a nicely formed table. If I just type estimates I get what looks like the beginning of estimates // Normal. That is it starts with {<|Business ID -> TYPR1206, . Although that might be because I have too many keys. $\endgroup$ Commented Aug 6, 2014 at 20:43
  • $\begingroup$ So that we both might learn would you be able to upload your full dataset code somewhere, like pastebin? $\endgroup$
    – Mr.Wizard
    Commented Aug 6, 2014 at 20:44
  • $\begingroup$ I added the code and a truncated sanitized version of the Excel sheet to my question. Does that help? $\endgroup$ Commented Aug 6, 2014 at 20:56

3 Answers 3

6
$\begingroup$

You can simply do:

titanic = ExampleData[{"Dataset", "Titanic"}];

Then:

titanic[1, Keys]

Mathematica graphics

OR

Query[1, Keys] @ titanic 

You can convert to List with Normal e.g.:

titanic[1, Keys] // Normal

{"class", "age", "sex", "survived"}

Another variation:

Normal @ Keys @ titanic[[1]]
$\endgroup$
4
  • $\begingroup$ That only seems to work when there aren't too many keys. I have another dataset with only 9 keys and your solution works fine there. There doesn't seem to be an option to tell Mathematica to expand the output similar to what it does when you ask it to show you a very large list. $\endgroup$ Commented Aug 6, 2014 at 20:33
  • $\begingroup$ @Mitchell titanic[1, Keys] // Normal should work. The key is Normal to convert to a standard List format. The truncation you see without it is part of the formatting of Dataset. $\endgroup$
    – Mr.Wizard
    Commented Aug 6, 2014 at 20:34
  • $\begingroup$ Oh! That does make the difference. $\endgroup$ Commented Aug 6, 2014 at 20:36
  • $\begingroup$ @MitchellKaplan, just add // Normal at the end of both methods. See my edit. $\endgroup$
    – RunnyKine
    Commented Aug 6, 2014 at 20:36
7
$\begingroup$

To get all Keys for a Dataset named dataset, where the first entry doesn't include all Keys

dataset[Keys /* Flatten /* Union] // Normal

will reveal all Keys, except those of nested Associations

$\endgroup$
5
  • 2
    $\begingroup$ This seems like the best answer but I think Keys[#] & may be replaced with simply Keys. $\endgroup$
    – Mr.Wizard
    Commented Aug 6, 2014 at 21:39
  • $\begingroup$ @Mr.Wizard, you're correct and I changed it. But I'm surprised, that this even works for Datasets where dataset[Keys] fails. $\endgroup$
    – Karsten7
    Commented Aug 6, 2014 at 21:50
  • $\begingroup$ There are lots of bugs to be worked out. It's quite clear that v10 was a bit rushed. $\endgroup$
    – Mr.Wizard
    Commented Aug 6, 2014 at 22:13
  • $\begingroup$ I've been using the Prefix or Postfix version of this in 10.0.1: Normal@Union@Flatten@Keys@dataset or dataset//Keys//Flatten//Union//Normal. $\endgroup$
    – Joe Fusion
    Commented Nov 19, 2014 at 1:26
  • $\begingroup$ FWIW: Using DeleteDuplicates instead of Union preserves the ordering of the keys. $\endgroup$
    – kjo
    Commented Dec 3, 2016 at 18:03
4
$\begingroup$

On the example Datasets in the documentation dataset[Keys] fails with an error, but I believe you can use this:

titanic = ExampleData[{"Dataset", "Titanic"}];

titanic // First // Keys // Normal
{"class", "age", "sex", "survived"}

I should have emphasized that the most important function here is Normal, which will convert output to a standard List. This prevents the automatic formatting used for Dataset from truncating your output, and also lets it be handled as a common list by other functions.

$\endgroup$
1
  • $\begingroup$ Works perfectly, thanks. $\endgroup$ Commented Aug 6, 2014 at 20:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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