Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

share|improve this question
    
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? –  Mr.Wizard Aug 6 at 20:21
    
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. –  Mitchell Kaplan Aug 6 at 20:43
    
So that we both might learn would you be able to upload your full dataset code somewhere, like pastebin? –  Mr.Wizard Aug 6 at 20:44
    
I added the code and a truncated sanitized version of the Excel sheet to my question. Does that help? –  Mitchell Kaplan Aug 6 at 20:56

3 Answers 3

up vote 5 down vote accepted

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]]
share|improve this answer
    
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. –  Mitchell Kaplan Aug 6 at 20:33
    
@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. –  Mr.Wizard Aug 6 at 20:34
    
Oh! That does make the difference. –  Mitchell Kaplan Aug 6 at 20:36
    
@MitchellKaplan, just add // Normal at the end of both methods. See my edit. –  RunnyKine Aug 6 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

share|improve this answer
2  
This seems like the best answer but I think Keys[#] & may be replaced with simply Keys. –  Mr.Wizard Aug 6 at 21:39
    
@Mr.Wizard, you're correct and I changed it. But I'm surprised, that this even works for Datasets where dataset[Keys] fails. –  Karsten 7. Aug 6 at 21:50
    
There are lots of bugs to be worked out. It's quite clear that v10 was a bit rushed. –  Mr.Wizard Aug 6 at 22:13
    
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. –  Joe Fusion yesterday

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.

share|improve this answer
    
Works perfectly, thanks. –  Mitchell Kaplan Aug 6 at 20:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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