Deleting all keys that have null values
Clash 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?
associations
add a comment |Â
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?
associations
add a comment |Â
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?
associations
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
associations
edited 2 days ago
asked Sep 9 at 16:51


kickert
53015
53015
add a comment |Â
add a comment |Â
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.
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
add a comment |Â
up vote
6
down vote
Select
:
Select[# != "" &] /@ test
"A" -> 1, "B" -> 2
DeleteMissing
:
DeleteMissing[test /. "" -> Missing, 2]
<
AssociationMap
:
AssociationMap[If[#[[2]] == "", Nothing, #] &] /@ test
>
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
add a comment |Â
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.
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
add a comment |Â
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.
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
add a comment |Â
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.
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.
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
add a comment |Â
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
add a comment |Â
up vote
6
down vote
Select
:
Select[# != "" &] /@ test
"A" -> 1, "B" -> 2
DeleteMissing
:
DeleteMissing[test /. "" -> Missing, 2]
<
AssociationMap
:
AssociationMap[If[#[[2]] == "", Nothing, #] &] /@ test
>
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
add a comment |Â
up vote
6
down vote
Select
:
Select[# != "" &] /@ test
"A" -> 1, "B" -> 2
DeleteMissing
:
DeleteMissing[test /. "" -> Missing, 2]
<
AssociationMap
:
AssociationMap[If[#[[2]] == "", Nothing, #] &] /@ test
>
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
add a comment |Â
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
>
Select
:
Select[# != "" &] /@ test
"A" -> 1, "B" -> 2
DeleteMissing
:
DeleteMissing[test /. "" -> Missing, 2]
<
AssociationMap
:
AssociationMap[If[#[[2]] == "", Nothing, #] &] /@ test
>
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
add a comment |Â
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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password