1
$\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$
2
  • $\begingroup$ Try ds[All, "b"]. It's alright to have a Missing[] entry. $\endgroup$ Commented Aug 21, 2017 at 13:07
  • 1
    $\begingroup$ This extract "b" only.I want the whole row. $\endgroup$
    – erow
    Commented Aug 21, 2017 at 13:29

2 Answers 2

3
$\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$
3
$\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$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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