From 6fe75a67893783157ab2fb33686ea89ae7ee249a Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Sun, 7 Oct 2018 19:31:54 -0500 Subject: [PATCH] DOC: Add cookbook entry for triangular correlation matrix (GH22840) --- doc/source/cookbook.rst | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/doc/source/cookbook.rst b/doc/source/cookbook.rst index be8457fc14a4f..21d1f11ba49ba 100644 --- a/doc/source/cookbook.rst +++ b/doc/source/cookbook.rst @@ -1226,6 +1226,17 @@ Computation Correlation *********** +Often it's useful to obtain the lower (or upper) triangular form of a correlation matrix calculated from :func:`DataFrame.corr`. This can be achieved by passing a boolean mask to ``where`` as follows: + +.. ipython:: python + + df = pd.DataFrame(np.random.random(size=(100, 5))) + + corr_mat = df.corr() + mask = np.tril(np.ones_like(corr_mat, dtype=np.bool), k=-1) + + corr_mat.where(mask) + The `method` argument within `DataFrame.corr` can accept a callable in addition to the named correlation types. Here we compute the `distance correlation `__ matrix for a `DataFrame` object. .. code-block:: python