# Delete columns of table where the sum of column adds to zero

Given,

data = {{1, 0, 1, 0, 1}, {2, 0, 2, 0, 2}, {3, 0, 3, 0, 3}, {4, 0, 4,
0, 4}, {5, 0, 5, 0, 5}};


Delete the columns whose sum totals to zero. Specifically, columns 2 and 4. I have tried creating a list of the sums:

list = Total[data, {1}];
{15, 0, 15, 0, 15}


and then summing through this list and deleting the cases in "data" where its total is zero. However, this is not working. If anyone could help me with this problem, I would appreciate it.

• Transpose@Pick[Transpose@data, Map[Positive, list]] Jul 19, 2021 at 21:24
• Yoou could do Transpose[Select[Transpose[data], Total@# != 0 &]] (Edit: Domen beat me to it by 4 seconds!) Jul 19, 2021 at 21:24
• Thank you, this worked! Jul 19, 2021 at 21:35
• Transpose@data /. x_ /; Total[x] == 0 -> Nothing // Transpose
– Syed
Sep 16, 2021 at 17:42
• Map[Cases[#, x_ /; x > 0] &, data] Apr 18, 2022 at 7:15

f1 = Transpose @* Select[UnequalTo[0] @* Tr] @* Transpose;

f1 @ data

{{1, 1, 1}, {2, 2, 2}, {3, 3, 3}, {4, 4, 4}, {5, 5, 5}}

f2 = Transpose @* DeleteCases[_?(EqualTo[0] @* Tr)] @* Transpose;

f2 @ data

{{1, 1, 1}, {2, 2, 2}, {3, 3, 3}, {4, 4, 4}, {5, 5, 5}}

• particularly like f1 +1 :) Apr 18, 2022 at 8:41

And another fun one

Drop[data,
None, {##, 2} & @@ Flatten@Position[Total@data, 0]] // MatrixForm


Another possibility is to use SequenceCases:

data = {{1, 0, 1, 0, 1}, {2, 0, 2, 0, 2}, {3, 0, 3, 0, 3}, {4, 0, 4, 0, 4}, {5, 0, 5, 0, 5}};


Transpose[Map[If[SequenceCases[#, list_ /; Total[list] =!= 0] === {}, Nothing, #] &, Transpose[data]]]


Another approach is the following:

data = {{1, 0, 1, 0, 1}, {2, 0, 2, 0, 2}, {3, 0, 3, 0, 3}, {4, 0, 4, 0, 4}, {5, 0, 5, 0, 5}};


Transpose[Map[If[Total[data[[All, #]]] === 0, Nothing, data[[All, #]]] &, Range[Length[data]]]]


Another possibility is to use Table:

data = {{1, 0, 1, 0, 1}, {2, 0, 2, 0, 2}, {3, 0, 3, 0, 3}, {4, 0, 4, 0, 4}, {5, 0, 5, 0, 5}};


Transpose[Table[If[Total[data[[All, i]]] > 0, data[[All, i]], Nothing], {i, 1, Length[data]}]]


data = {{1, 0, 1, 0, 1}, {2, 0, 2, 0, 2}, {3, 0, 3, 0, 3}, {4, 0, 4,
0, 4}, {5, 0, 5, 0, 5}};

pos = Flatten@Position[Total[data], 0]


{2, 4}

data[[All, pos]] = Nothing


Result:

{{1, 1, 1}, {2, 2, 2}, {3, 3, 3}, {4, 4, 4}, {5, 5, 5}}

• Hi @Syed! Welcome to the party called by @Nasser :) Apr 18, 2022 at 8:12

We have

data = {{1, 0, 1, 0, 1}, {2, 0, 2, 0, 2}, {3, 0, 3, 0, 3}, {4, 0, 4,
0, 4}, {5, 0, 5, 0, 5}};

data // MatrixForm


1. Use Position and Total to locate which columns you want removed and then Delete to delete them.

The code is:

Transpose@
Delete[Transpose@data, Position[Total@data, 0]] // MatrixForm


1. This is a minor comment. You can use Select and Total as was suggested in the comment but there's no need for Transpose.

The code is:

Select[Total@# != 0 &] /@ data // MatrixForm


Just for fun, another possibility is to use MapIndexed (there has to be at least 10 different ways to solve the same problem in Mathematica. I counted only 7 so far (including in comments). This forum needs to become more active :)

data = {{1, 0, 1, 0, 1}, {2, 0, 2, 0, 2}, {3, 0, 3, 0, 3}, {4, 0, 4, 0, 4}, {5, 0, 5, 0, 5}};


sum = Total[data, {1}]
MapIndexed[If[#1 == 0, data[[All, First[#2]]] = Sequence[], Nothing] &, sum];


• Most of the times, I keep forgetting about our friend the MapIndexed. Not sure why. Nicely done!!!
– bmf
Apr 18, 2022 at 0:16
• @bmf the mapindexed is like the enumerate in python :). which will give you the element and index at the same time. Apr 18, 2022 at 6:53
• @AsukaMinato not a python user, but thanks for letting me know. At some point I have to teach myself :) I am aware what MapIndexed does, but for some reason it will slip my mind most of the times...my dysfunctional brain :)
– bmf
Apr 18, 2022 at 14:21

Since @Nasser wrote the infamous statement about the 10 ways, I cannot resist.

Drop[data, None,
Append[2]@Flatten@Position[Total@data, 0]] // MatrixForm