8
$\begingroup$

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?

$\endgroup$
6
$\begingroup$

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.

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

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

$\endgroup$
  • 2
    $\begingroup$ For some of my more complicated datasets, this actually provides me with even more functionality. Thanks for the suggestion. $\endgroup$ – kickert Sep 9 '18 at 17:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.