1
$\begingroup$

I have imported data. {{...},{...},{...},...{...}}

Here a screenshot: enter image description here

As you can see there are empty entires {,,,,,,}

How can I delete those entries ?

$\endgroup$
3
$\begingroup$

If you want to delete rows that only consist of empty strings:

DeleteCases[data, {"" ..}]

"" .. is a pattern for "1 or more empty strings in a row". .. is shorthand for Repeated.

$\endgroup$
  • $\begingroup$ Very elegant ! Good explenation! Thank you. $\endgroup$ – henry Aug 9 '16 at 15:35
  • $\begingroup$ DeleteCases[{{1,""}, {2,""}, {}}, {"" ..}] doesn't give me the same result as I have in my answer. Perhaps I am missing something. $\endgroup$ – Eric Brown Aug 9 '16 at 15:44
  • $\begingroup$ @Gerli: If I want to delete rows with Complex Number, then how do you revise your code above? $\endgroup$ – Tugrul Temel Dec 16 '19 at 19:53
  • 1
    $\begingroup$ @TugrulTemel if you want to delete rows that only have complex numbers then instead of "" use _Complex as the pattern for the element to delete (ending up with {_Complex ..} -- or you can use {__Complex} which is equivalent in this case). From the wording of your question it seems like you might want to delete rows that contain a complex number, in which case the pattern you want could be {___, _Complex, ___} (0 or more elements, then a complex number, then 0 or more elements in the list) $\endgroup$ – Gerli Dec 18 '19 at 7:25
  • $\begingroup$ @Gerli: I wanted to delete the rows with complex numbers, and your suggestion works fine. Thanks for your reply. $\endgroup$ – Tugrul Temel Dec 18 '19 at 13:50
1
$\begingroup$

Those are probably empty strings, so:

DeleteCases[{1,"",""}, ""]

will probably work for a single list. For lists of lists, you can use Map:

Map[ DeleteCases[#, ""] &, listOfLists]

If you would like to delete remaining empty rows, you can augment this with another DeleteCases:

DeleteCases[
  Map[ DeleteCases[#, ""] &, listOfLists]
, {}]
$\endgroup$
  • $\begingroup$ Hmm ... it is not working. It is working for your problem, but in my case I have rows witch are only empty strings. $\endgroup$ – henry Aug 9 '16 at 15:20
  • $\begingroup$ Please try my edited response. $\endgroup$ – Eric Brown Aug 9 '16 at 15:24
  • $\begingroup$ Very close... it delets all the empty entries but leaves the brakets {}. I have found a way to get rid of them. DeleteCases[Map[DeleteCases[#, ""] &, resMat], {}] $\endgroup$ – henry Aug 9 '16 at 15:27
  • $\begingroup$ please add my solution to your response, so that I can validate your answer. :) $\endgroup$ – henry Aug 9 '16 at 15:28
  • $\begingroup$ I made the edit. $\endgroup$ – Eric Brown Aug 9 '16 at 15:34

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.