0
$\begingroup$

I collect some data with missing items.

data = Dataset[{<|"a" -> 1, "b" -> 2|>, <|"a" -> 11|>}];

It's not right to do data[;; , #b &].

So how to set a default value for b as zero?

Or delete the row missing b?

$\endgroup$
  • $\begingroup$ Try ds[All, "b"]. It's alright to have a Missing[] entry. $\endgroup$ – J. M. will be back soon Aug 21 '17 at 13:07
  • $\begingroup$ This extract "b" only.I want the whole row. $\endgroup$ – erow Aug 21 '17 at 13:29
2
$\begingroup$

You may use the FailureAction option of Query to specify an action for a failure.

With data as defined in OP.

"Drop" items that fail.

data[;; , #b &, FailureAction -> "Drop" ] // Normal
{2}

"Replace" failed items with a placeholder

data[;; , #b &, FailureAction -> "Replace" ] // Normal
{2, Missing["Failed"]}

Replace failed items with a default

data[;; , #b &, FailureAction -> {"Replace", 0 &} ] // Normal
{2, 0}

Return failure objects

data[;; , #b &, FailureAction -> "Encapsulate" ] // Normal
{2, Failure[...]}

Hope this helps

$\endgroup$
1
$\begingroup$

How about

data[All, {"a", "b"}] /. _Missing -> 0

Mathematica graphics

which contains a value of 0 for b whenever it was missing before:

Normal @ %
(* Out: {<|"a" -> 1, "b" -> 2|>, <|"a" -> 11, "b" -> 0|>} *)
$\endgroup$

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.