8

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 = {<|"A" -> 1, "B" -> 2, "C" -> ""|>, <|"A" -> 3, "B" -> 4, 
   "C" -> 5|>, <|"A" -> "", "B" -> 6, "C" -> 7|>}

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

test2 = {<|"A" -> 1, "B" -> 2|>, <|"A" -> 3, "B" -> 4, "C" -> 5|>, <|
   "B" -> 6, "C" -> 7|>}

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?

kickert
  • 1,820
  • 8
  • 22

2 Answers2

7

Try this:

DeleteCases[test, "", {2}]

{<|"A" -> 1, "B" -> 2|>, <|"A" -> 3, "B" -> 4, "C" -> 5|>, <|"B" -> 6, "C" -> 7|>}

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.

Henrik Schumacher
  • 106,770
  • 7
  • 179
  • 309
  • Ahh... that makes sense. My earlier attempts didn't take into consideration the level of the key and value. – kickert Sep 09 '18 at 17:20
7

Select:

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

{<|"A" -> 1, "B" -> 2|>, <|"A" -> 3, "B" -> 4, "C" -> 5|>, <|"B" -> 6,    "C" -> 7|>}

DeleteMissing:

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

{<|"A" -> 1, "B" -> 2|>, <|"A" -> 3, "B" -> 4, "C" -> 5|>, <| "B" -> 6,   "C" -> 7|>}

AssociationMap:

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

{<|"A" -> 1, "B" -> 2|>, <|"A" -> 3, "B" -> 4, "C" -> 5|>, <| "B" -> 6, "C" -> 7|>}

kglr
  • 394,356
  • 18
  • 477
  • 896
  • 2
    For some of my more complicated datasets, this actually provides me with even more functionality. Thanks for the suggestion. – kickert Sep 09 '18 at 17:21