Converting the datasubset

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",{

{year: "2019", impact:0},

{year: "2020", impact:0},

{year: "2021", impact:0},

{year: "2022", impact:0},

{year: "2023", impact:0}

}}

}

Is it possible to do?

Thanks

  Discussion posts and replies are publicly visible

Parents
  • Hey ,

    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
              }
            )
          }
        }
      )
    )

Reply
  • Hey ,

    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
              }
            )
          }
        }
      )
    )

Children
No Data