In [1]:
from scipy.sparse import csr_matrix, triu, tril

In [2]:
l = [[0, 1, 2],
     [3, 0, 4],
     [5, 6, 7]]

In [3]:
csr = csr_matrix(l)

In [4]:
print(triu(csr).toarray())


[[0 1 2]
 [0 0 4]
 [0 0 7]]

In [5]:
print(type(triu(csr)))


<class 'scipy.sparse.coo.coo_matrix'>

In [6]:
print(type(triu(csr, format='csr')))


<class 'scipy.sparse.csr.csr_matrix'>

In [7]:
print(triu(csr, k=1).toarray())


[[0 1 2]
 [0 0 4]
 [0 0 0]]

In [8]:
print(triu(csr, k=-1).toarray())


[[0 1 2]
 [3 0 4]
 [0 6 7]]

In [9]:
print(tril(csr).toarray())


[[0 0 0]
 [3 0 0]
 [5 6 7]]

In [10]:
print(type(tril(csr)))


<class 'scipy.sparse.coo.coo_matrix'>

In [11]:
print(type(tril(csr, format='csr')))


<class 'scipy.sparse.csr.csr_matrix'>

In [12]:
print(tril(csr, k=1).toarray())


[[0 1 0]
 [3 0 4]
 [5 6 7]]

In [13]:
print(tril(csr, k=-1).toarray())


[[0 0 0]
 [3 0 0]
 [5 6 0]]