Group Abstract Group Abstract

Message Boards Message Boards

0
|
16.6K Views
|
13 Replies
|
12 Total Likes
View groups...
Share
Share this post:

Convert the following list into a Dataset?

The first row should be treated as a field name instead of data.

temp = {{a, b, c}, {1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
Dataset[temp]
POSTED BY: Tsai Ming-Chou
13 Replies

Most likely there is a simpler way, but here is a first attempt :

temp = {{a, b, c}, {1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
keys = ToString /@ First[temp];
values = Rest[temp];
dataset = Dataset[Association /@ (MapThread[Rule, {keys, #}] & /@ values)]

which gives

enter image description here

Regards -- Henrik

POSTED BY: Henrik Schachner

Given:

temp = {{a, b, c}, {1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

Here are some additional styles of solving it. Here we see that we can use <| |> In place of Association[] for a little shorter notation:

<|ToString /@ First@temp -> Transpose@Rest@temp // Thread|>
(* <|"a" -> {1, 4, 7}, "b" -> {2, 5, 8}, "c" -> {3, 6, 9}|> *)

Also if you don't like having too many # and & in your code, you can use the operator form of Map and also Curry which was introduced in version 11.3:

Transpose@Rest@temp //
   Map[Curry[AssociationThread, 2][ToString /@ First@temp]]
(*{<|"a" -> 1, "b" -> 4, "c" -> 7|>,
   <|"a" -> 2, "b" -> 5, "c" -> 8|>,
   <|"a" -> 3, "b" -> 6, "c" -> 9|>}*)
POSTED BY: Gustavo Delfino

These two approaches give the same result:

AssociationThread[keys -> #] & /@ values
(*{<|"a" -> 1, "b" -> 2, "c" -> 3|>, <|"a" -> 4, "b" -> 5, "c" -> 6|>, <|"a" -> 7, "b" -> 8, "c" -> 9|>}*)

Association /@ (MapThread[Rule, {keys, #}] & /@ values)
(*{<|"a" -> 1, "b" -> 2, "c" -> 3|>, <|"a" -> 4, "b" -> 5, "c" -> 6|>, <|"a" -> 7, "b" -> 8, "c" -> 9|>}*)

Three approaches:

SemanticImportString[ExportString[temp, "CSV"]]
Posted 8 years ago
temp=AssociationThread[ToString /@ {a, b, c} -> #] & /@ {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
Dataset@temp
POSTED BY: Girish Arabale
Posted 8 years ago
POSTED BY: Girish Arabale

First of all, thank for your generous help! From everyone's suggestion, I try various methods and observe what is different!

temp = {{a, b, c}, {1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
keys = ToString /@ First@temp;
values = Rest@temp;
Dataset[Association /@ MapThread[Rule, {keys, #}] & /@ values]
Dataset[Association /@ (MapThread[Rule, {keys, #}] & /@ values)]

The second dataset can match my imagination of structural data. But if keys = First@temp;

keys = First@temp;
Dataset[Association /@ MapThread[Rule, {keys, #}] & /@ values]
Dataset[Association /@ (MapThread[Rule, {keys, #}] & /@ values)]

Although there is no difference in the appearance of the dataset, the actual structure is not the same.

{{<|a -> 1|>, <|b -> 2|>, <|c -> 3|>}, {<|a -> 4|>, <|b -> 5|>, <|c -> 6|>}, {<|a -> 7|>, <|b -> 8|>, <|c -> 9|>}}

{<|a -> 1, b -> 2, c -> 3|>, <|a -> 4, b -> 5, c -> 6|>, <|a -> 7, b -> 8, c -> 9|>}

POSTED BY: Tsai Ming-Chou

Thank you for your help, the code seems to be really short and effective. But for a beginner, the logic between the instructions, I still need to learn more. Especially the use of Association, Map, Replace and Rule.It is really a headache.

POSTED BY: Tsai Ming-Chou
Posted 8 years ago
pos = Values[Normal[aqi[All, {"Latitude", "Longitude", "AQI"}]]] /. {lat_, long_, aqi_} :> Thread[GeoPosition[{lat, long}] -> aqi];

GeoSmoothHistogram[Association[pos], ColorFunction -> "Rainbow", 
Mesh -> 20, MeshStyle -> Directive[Opacity[0.085], Blue], 
GeoBackground -> "StreetMap"]

enter image description here

You need to generate GeoElevationData :

   ged= GeoElevationData /@(Values[Normal[aqi[All, {"Latitude", "Longitude"}]]] /. {lat_, long_} :> GeoPosition[{lat, long}])

Check with GeoBackground -> "ReliefMap" option in the above plot. Play with GeoElevationData too. Have been just throwing quick solutions while commuting...

POSTED BY: Girish Arabale

If I need to use GeoSmoothHistogram[locs,espec], I want to use GeoPosition and AQI indicator (aqi [[;;, 3]]). What should I do?Is it possible to consider GeoElevationData at the same time?

POSTED BY: Tsai Ming-Chou
POSTED BY: Tsai Ming-Chou
POSTED BY: Neil Singer
Posted 8 years ago

@Henrik Schachner : Not at all - the more the merrier!

POSTED BY: Girish Arabale

@Girish Arabale : Sorry, I did not see your answer when I posted mine. Using AssociationThread gives definitely a better solution!

POSTED BY: Henrik Schachner
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard