Question: I have a binary tsv matrix of the style:

```
name product 141 142 143 144 145 146 147 
n1   p1      0   0    0   0   0   1   1 
n2   p2      1   1    1   1   1   1   0
n3   p3      1   0    1   1   0   1   1

```

How can I sort this matrix in descending order by row sum? (starting at line 2, sum of 3rd column to last column) Please ask if you need more details or would like a sample file.

Answer: 
You can use Pandas.

```
import pandas as pd

file_name = "..."
df = pd.read_csv(file_name)

df = (df.set_index(['name', 'product'])  # Use `name` and `product` as index.
        .assign(sum=df.sum(axis=1))  # Add temporary 'sum' column to sum rows.
        .sort_values(by='sum', ascending=False)  # Sort by row sum descending order.
        .iloc[:, :-1])  # Remove temporary `sum` column.

>>> df
              141  142  143  144  145  146  147
name product                                   
n1   p1         0    0    0    0    0    1    1
n2   p2         1    1    1    1    1    1    0
n3   p3         1    0    1    1    0    1    1

```
This awk one-liner may help:

```
awk '{for(x=3;x<=NF;x++)s+=$x;print s,$0;s=0}' file|sort -nr|sed 's/^\S* //'

```

Output:

```
name product 141 142 143 144 145 146 147
n2   p2      1   1    1   1   1   1   0
n3   p3      1   0    1   1   0   1   1
n1   p1      0   0    0   0   0   1   1

```