Deleting all keys that have null values

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
8
down vote

favorite












I have a notebook that I am working on that starts with an imported CSV file. Many of the rows have blank data in them. I have used the following command to establish a list of associations:



test = AssociationThread[First[rawdata] -> #] & /@ Rest[rawdata]


The InputForm of the above code produces an end result that is something like this:



test = >, <


I want to remove the associations that have blank values so my end result would be more like this:



test2 = "A" -> 1, "B" -> 2


Is there a simple way to drop all keys across all the entries that have null values? I have looked into KeyDrop / KeyDropFrom but can't figure out a way to drop a key based on its value.



Based on this thread, I am thinking there may be a way to use Position or PositionIndex.



Thoughts?










share|improve this question



























    up vote
    8
    down vote

    favorite












    I have a notebook that I am working on that starts with an imported CSV file. Many of the rows have blank data in them. I have used the following command to establish a list of associations:



    test = AssociationThread[First[rawdata] -> #] & /@ Rest[rawdata]


    The InputForm of the above code produces an end result that is something like this:



    test = >, <


    I want to remove the associations that have blank values so my end result would be more like this:



    test2 = "A" -> 1, "B" -> 2


    Is there a simple way to drop all keys across all the entries that have null values? I have looked into KeyDrop / KeyDropFrom but can't figure out a way to drop a key based on its value.



    Based on this thread, I am thinking there may be a way to use Position or PositionIndex.



    Thoughts?










    share|improve this question

























      up vote
      8
      down vote

      favorite









      up vote
      8
      down vote

      favorite











      I have a notebook that I am working on that starts with an imported CSV file. Many of the rows have blank data in them. I have used the following command to establish a list of associations:



      test = AssociationThread[First[rawdata] -> #] & /@ Rest[rawdata]


      The InputForm of the above code produces an end result that is something like this:



      test = >, <


      I want to remove the associations that have blank values so my end result would be more like this:



      test2 = "A" -> 1, "B" -> 2


      Is there a simple way to drop all keys across all the entries that have null values? I have looked into KeyDrop / KeyDropFrom but can't figure out a way to drop a key based on its value.



      Based on this thread, I am thinking there may be a way to use Position or PositionIndex.



      Thoughts?










      share|improve this question















      I have a notebook that I am working on that starts with an imported CSV file. Many of the rows have blank data in them. I have used the following command to establish a list of associations:



      test = AssociationThread[First[rawdata] -> #] & /@ Rest[rawdata]


      The InputForm of the above code produces an end result that is something like this:



      test = >, <


      I want to remove the associations that have blank values so my end result would be more like this:



      test2 = "A" -> 1, "B" -> 2


      Is there a simple way to drop all keys across all the entries that have null values? I have looked into KeyDrop / KeyDropFrom but can't figure out a way to drop a key based on its value.



      Based on this thread, I am thinking there may be a way to use Position or PositionIndex.



      Thoughts?







      associations






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 2 days ago

























      asked Sep 9 at 16:51









      kickert

      53015




      53015




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          6
          down vote



          accepted










          Try this:



          DeleteCases[test, "", 2]



          >, <




          Since this is about a list of associations, the level on which to search the pattern "" is level 2 (only), therefore the 2 as third argument.






          share|improve this answer






















          • Ahh... that makes sense. My earlier attempts didn't take into consideration the level of the key and value.
            – kickert
            Sep 9 at 17:20

















          up vote
          6
          down vote













          Select:



          Select[# != "" &] /@ test



          "A" -> 1, "B" -> 2




          DeleteMissing:



          DeleteMissing[test /. "" -> Missing, 2]



          <




          AssociationMap:



          AssociationMap[If[#[[2]] == "", Nothing, #] &] /@ test



          >







          share|improve this answer


















          • 2




            For some of my more complicated datasets, this actually provides me with even more functionality. Thanks for the suggestion.
            – kickert
            Sep 9 at 17:21










          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "387"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f181559%2fdeleting-all-keys-that-have-null-values%23new-answer', 'question_page');

          );

          Post as a guest






























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          6
          down vote



          accepted










          Try this:



          DeleteCases[test, "", 2]



          >, <




          Since this is about a list of associations, the level on which to search the pattern "" is level 2 (only), therefore the 2 as third argument.






          share|improve this answer






















          • Ahh... that makes sense. My earlier attempts didn't take into consideration the level of the key and value.
            – kickert
            Sep 9 at 17:20














          up vote
          6
          down vote



          accepted










          Try this:



          DeleteCases[test, "", 2]



          >, <




          Since this is about a list of associations, the level on which to search the pattern "" is level 2 (only), therefore the 2 as third argument.






          share|improve this answer






















          • Ahh... that makes sense. My earlier attempts didn't take into consideration the level of the key and value.
            – kickert
            Sep 9 at 17:20












          up vote
          6
          down vote



          accepted







          up vote
          6
          down vote



          accepted






          Try this:



          DeleteCases[test, "", 2]



          >, <




          Since this is about a list of associations, the level on which to search the pattern "" is level 2 (only), therefore the 2 as third argument.






          share|improve this answer














          Try this:



          DeleteCases[test, "", 2]



          >, <




          Since this is about a list of associations, the level on which to search the pattern "" is level 2 (only), therefore the 2 as third argument.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Sep 9 at 17:41

























          answered Sep 9 at 17:01









          Henrik Schumacher

          37.5k249107




          37.5k249107











          • Ahh... that makes sense. My earlier attempts didn't take into consideration the level of the key and value.
            – kickert
            Sep 9 at 17:20
















          • Ahh... that makes sense. My earlier attempts didn't take into consideration the level of the key and value.
            – kickert
            Sep 9 at 17:20















          Ahh... that makes sense. My earlier attempts didn't take into consideration the level of the key and value.
          – kickert
          Sep 9 at 17:20




          Ahh... that makes sense. My earlier attempts didn't take into consideration the level of the key and value.
          – kickert
          Sep 9 at 17:20










          up vote
          6
          down vote













          Select:



          Select[# != "" &] /@ test



          "A" -> 1, "B" -> 2




          DeleteMissing:



          DeleteMissing[test /. "" -> Missing, 2]



          <




          AssociationMap:



          AssociationMap[If[#[[2]] == "", Nothing, #] &] /@ test



          >







          share|improve this answer


















          • 2




            For some of my more complicated datasets, this actually provides me with even more functionality. Thanks for the suggestion.
            – kickert
            Sep 9 at 17:21














          up vote
          6
          down vote













          Select:



          Select[# != "" &] /@ test



          "A" -> 1, "B" -> 2




          DeleteMissing:



          DeleteMissing[test /. "" -> Missing, 2]



          <




          AssociationMap:



          AssociationMap[If[#[[2]] == "", Nothing, #] &] /@ test



          >







          share|improve this answer


















          • 2




            For some of my more complicated datasets, this actually provides me with even more functionality. Thanks for the suggestion.
            – kickert
            Sep 9 at 17:21












          up vote
          6
          down vote










          up vote
          6
          down vote









          Select:



          Select[# != "" &] /@ test



          "A" -> 1, "B" -> 2




          DeleteMissing:



          DeleteMissing[test /. "" -> Missing, 2]



          <




          AssociationMap:



          AssociationMap[If[#[[2]] == "", Nothing, #] &] /@ test



          >







          share|improve this answer














          Select:



          Select[# != "" &] /@ test



          "A" -> 1, "B" -> 2




          DeleteMissing:



          DeleteMissing[test /. "" -> Missing, 2]



          <




          AssociationMap:



          AssociationMap[If[#[[2]] == "", Nothing, #] &] /@ test



          >








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Sep 9 at 19:17

























          answered Sep 9 at 17:17









          kglr

          159k8184384




          159k8184384







          • 2




            For some of my more complicated datasets, this actually provides me with even more functionality. Thanks for the suggestion.
            – kickert
            Sep 9 at 17:21












          • 2




            For some of my more complicated datasets, this actually provides me with even more functionality. Thanks for the suggestion.
            – kickert
            Sep 9 at 17:21







          2




          2




          For some of my more complicated datasets, this actually provides me with even more functionality. Thanks for the suggestion.
          – kickert
          Sep 9 at 17:21




          For some of my more complicated datasets, this actually provides me with even more functionality. Thanks for the suggestion.
          – kickert
          Sep 9 at 17:21

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f181559%2fdeleting-all-keys-that-have-null-values%23new-answer', 'question_page');

          );

          Post as a guest













































































          Comments

          Popular posts from this blog

          What does second last employer means? [closed]

          List of Gilmore Girls characters

          Confectionery