Message Boards Message Boards

Convert the following list into a Dataset?

GROUPS:

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
Answer
2 months ago
temp=AssociationThread[ToString /@ {a, b, c} -> #] & /@ {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
Dataset@temp
POSTED BY: Girish Arabale
Answer
2 months ago

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

POSTED BY: Henrik Schachner
Answer
2 months ago

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

POSTED BY: Girish Arabale
Answer
2 months ago

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
Answer
2 months ago

If your data is starting in a file (ie CSV or TSV file), you can go direct from file to dataset with SemanticImport[].

Regards

POSTED BY: Neil Singer
Answer
2 months ago

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
Answer
2 months ago

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 BY: Christopher French
Answer
2 months ago

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
Answer
2 months ago

It's amazing! Different instructions produce different data structures! I am an environmental epidemiology student and mathematica beginner. I would like to use public data to observe the distribution of air pollution concentrations but plagued by a large number of monitoring station coordinates and observations (CSV file). The coordinates and observations of these coordinates and observations are converted to GeoPosition or GeoShothistogram every day. Now that the CSV file has converted to Dataset, and establish a long-term database and the workload reduced. The next step is to convert the station coordinates to GeoPosition. The coordinate data is already known as aqi[[;;,{-2,-1}]]. How to put coordinates into GeoPosition?

   aqi = Import[
          "http://opendata.epa.gov.tw/ws/Data/AQI/?$format=csv", {"CSV", 
           "Dataset"}, HeaderLines -> 1];

I have tried the following instructions but it does not work

p={#Latitude, #Longitude} & /@ aqi;GeoListPlot[p]
POSTED BY: Tsai Ming-Chou
Answer
2 months ago

One way of doing I can think of quickly is:

p = aqi[All /* Catenate /* (Partition[#, 2] &) /* GeoPosition, {"Latitude", "Longitude"}]

which will create GeoPosition arrays. You can generate map:

GeoListPlot[p]

enter image description here

POSTED BY: Girish Arabale
Answer
2 months ago

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
Answer
2 months 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
Answer
2 months ago

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
Answer
2 months ago

Group Abstract Group Abstract