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:


which returned:

enter image description here

I also tried:


which returned:

enter image description here

How can I display the entire list of keys?

My code is below:

{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

  • $\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
    Aug 6 '14 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$ Aug 6 '14 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
    Aug 6 '14 at 20:44
  • $\begingroup$ I added the code and a truncated sanitized version of the Excel sheet to my question. Does that help? $\endgroup$ Aug 6 '14 at 20:56

You can simply do:

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


titanic[1, Keys]

Mathematica graphics


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]]
  • $\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$ Aug 6 '14 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
    Aug 6 '14 at 20:34
  • $\begingroup$ Oh! That does make the difference. $\endgroup$ Aug 6 '14 at 20:36
  • $\begingroup$ @MitchellKaplan, just add // Normal at the end of both methods. See my edit. $\endgroup$
    – RunnyKine
    Aug 6 '14 at 20:36

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

  • 2
    $\begingroup$ This seems like the best answer but I think Keys[#] & may be replaced with simply Keys. $\endgroup$
    – Mr.Wizard
    Aug 6 '14 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$
    – Karsten 7.
    Aug 6 '14 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
    Aug 6 '14 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
    Nov 19 '14 at 1:26
  • $\begingroup$ FWIW: Using DeleteDuplicates instead of Union preserves the ordering of the keys. $\endgroup$
    – kjo
    Dec 3 '16 at 18:03

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.

  • $\begingroup$ Works perfectly, thanks. $\endgroup$ Aug 6 '14 at 20:22

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.