Hi, I have a datasubset:
{{impact: 0.0, country_nm: "AA", year: "2019"},
{impact: 0.0, country_nm: "AA", year: "2020"},
{impact: 0.0, country_nm: "AA", year: "2021"},
{impact: 0.0, country_nm: "AA", year: "2022"},
{impact: 0.0, country_nm: "AA", year: "2023"},
{impact: 0.0, country_nm: "BB", year: "2019"},
{impact: 0.0, country_nm: "BB", year: "2020"},
{impact: 0.0, country_nm: "BB", year: "2021"},
{impact: 0.0, country_nm: "BB", year: "2022"},
{impact: 0.0, country_nm: "BB", year: "2023"}
},
And what I am trying to do is to convert it to different datasabsuet:
{
{country_nm: "AA",{
{year: "2019", impact:0},
{year: "2020", impact:0},
{year: "2021", impact:0},
{year: "2022", impact:0},
{year: "2023", impact:0}
}},
{country_nm: "BB",{
}}
}
Is it possible to do?
Thanks
Discussion posts and replies are publicly visible
FWIW, a!forEach is unexpectedly powerful, and can be nested pretty easily. This is what I was able to whip up quickly:
a!localVariables( local!originalData: { {impact: 0.0, country_nm: "AA", year: "2019"}, {impact: 0.0, country_nm: "AA", year: "2020"}, {impact: 0.0, country_nm: "AA", year: "2021"}, {impact: 0.0, country_nm: "AA", year: "2022"}, {impact: 0.0, country_nm: "AA", year: "2023"}, {impact: 0.0, country_nm: "BB", year: "2019"}, {impact: 0.0, country_nm: "BB", year: "2020"}, {impact: 0.0, country_nm: "BB", year: "2021"}, {impact: 0.0, country_nm: "BB", year: "2022"}, {impact: 0.0, country_nm: "BB", year: "2023"} }, local!uniqueCountries: union(local!originalData.country_nm, local!originalData.country_nm), a!forEach( local!uniqueCountries, a!localVariables( local!currentCountry: fv!item, { country_nm: fv!item, country_data: a!forEach( local!originalData, if( fv!item.country_nm = local!currentCountry, { year: fv!item.year, impact: fv!item.impact }, {} ) ) } ) ) )
it seems to do what you want, though in the transformed data set it's necessary to label the "year data" arrays i believe.
Hey ramanh,
Before the solution, I would like to tell you that the dataset you are trying to achieve requires a little modification. You are trying to create a syntactically incorrect dictionary because you mixed the keyword and non-keyword arguments/values. Ex: {country_nm: "BB",{year: "2019", impact:0}}. The first index of the dictionary has "country_nm" as key but the second index does not have any key. The required dataset can be achieved by adding a key to the second index. You can find a code below to achieve the same.
a!localVariables( local!dataSet: { {impact: 0.0, country_nm: "AA", year: "2019"}, {impact: 0.0, country_nm: "AA", year: "2020"}, {impact: 0.0, country_nm: "AA", year: "2021"}, {impact: 0.0, country_nm: "AA", year: "2022"}, {impact: 0.0, country_nm: "AA", year: "2023"}, {impact: 0.0, country_nm: "BB", year: "2019"}, {impact: 0.0, country_nm: "BB", year: "2020"}, {impact: 0.0, country_nm: "BB", year: "2021"}, {impact: 0.0, country_nm: "BB", year: "2022"}, {impact: 0.0, country_nm: "BB", year: "2023"} }, a!forEach( items: union(local!dataSet.country_nm, local!dataSet.country_nm), expression: { country_nm: fv!item, data:{ a!forEach( items: index(local!dataSet,wherecontains(fv!item,touniformstring(local!dataSet.country_nm)),{}), expression: { year: fv!item.year, impact: fv!item.impact } ) } } ) )