We can select values on the diagonal of a pandas DataFrame using the numpy
method diagonal()
. Here's an example:
main.py219 chars12 lines
This will output:
main.py17 chars2 lines
Note that this approach assumes that the DataFrame has a square shape (i.e. the number of rows and columns are equal). If the DataFrame is not square, you'll need to first extract the square subset of the DataFrame that contains the diagonal, and then apply np.diagonal()
.
gistlibby LogSnag