I have a square matrix s> 1000 rows and columns. Many fields on the border have nan , for example:
grid = [[nan, nan, nan, nan, nan], [nan, nan, nan, nan, nan], [nan, nan, 1, nan, nan], [nan, 2, 3, 2, nan], [ 1, 2, 2, 1, nan]]
Now I want to delete all rows and columns where I only have nan . This will be row 1. and 2. and the last column. But I also want to get a square matrix, so the number of rows excluded should be equal to the number of columns deleted. In this example, I want to get the following:
grid = [[nan, nan, nan, nan], [nan, nan, 1, nan], [nan, 2, 3, 2], [ 1, 2, 2, 1]]
I'm sure I can solve this with a loop: check each column and row if there is only nan inside, and in the end I use numpy.delete to delete the rows and columns found (but only the minimum number, due to getting the square). But I hope someone can help me with a better solution or a good library.