I'm trying to find a way to convert a GroupBy
field in a dataset into a column after aggregation, but I'm clearly failing to this idiomatically, although I found a couple of solutions, see below.
Consider the COVID-19 dataset:
covid = ResourceData["Epidemic Data for Novel Coronavirus COVID-19"]
and let's say I want to look at just the number of confirmed cases and deaths by country. It's easy to do in a very idiomatic way:
covid[GroupBy["Country"], Total, {"ConfirmedCases", "Deaths"}]
But let's say I now want Spain
etc. to be a column, not a row name. I can do it with KeyValueMap
, but it's a bit convoluted:
covid[GroupBy["Country"], Total, {"ConfirmedCases", "Deaths"}][
KeyValueMap[Join[<|"Country" -> #1|>, #2] &]]
Another idea I had was to look at
covid[GroupBy["Country"] /* Values, Total, {"Country", "ConfirmedCases",
"Deaths"}]
which unfortunately doesn't work as Country
is also aggregated,
and to try to fix it by applying each of the aggregating function in a separate query:
covid[GroupBy["Country"] /* Values, <|
"Country" -> First /* "Country",
"ConfirmedCases" -> Total /* "ConfirmedCases",
"Deaths" -> Total /* "Deaths"
|>]
which works as expected, but is also not very pleasant to the eye.
Am I missing something obvious here?
Transpose @ res[ GroupBy["Country"], Total, {"ConfirmedCases", "Deaths"} ]
? $\endgroup$<|"ConfirmedCases" -> <|Entity["Country", "Spain"] -> TimeSeries[...]...|>,...|>
$\endgroup$