Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting non-diagonal elements in dataframe

Following pandas DataFrame diagonal, I can get diagonal elements using np.diag. How can I get the non-diagonal elements in a dataframe (assuming that dataframe is size n x n)

like image 776
user308827 Avatar asked Jul 01 '17 01:07

user308827


People also ask

How do you find non-diagonal elements in a matrix?

So in order to get the sum of the non-diagonal parts of the matrix: Traverse the matrix rowwise. If the element is a part of diagonal, then skip this element. If the element is part of the left, right, bottom, or top part (i.e. non-diagonal parts), add the element in the resultant sum.

What are non-diagonal elements?

The elements which do not lie on the leading diagonal of a square matrix is called non-diagonal elements of the matrix. The number of rows is equal to the number of columns in a square matrix. So, a principal diagonal is formed by the first element of first row and last element of last row.

How do you find the diagonal of a element in Python?

With the help of Numpy matrix. diagonal() method, we are able to find a diagonal element from a given matrix and gives output as one dimensional matrix.


1 Answers

Use a mask generated with np.eye like:

xf = pd.DataFrame(np.random.rand(5,5))
xf.mask(np.eye(5, dtype = bool))
like image 106
Some Guy Avatar answered Sep 21 '22 08:09

Some Guy