Skip to content

Commit dda017f

Browse files
Daniel Saxtontm9k1
Daniel Saxton
authored andcommitted
DOC: Add cookbook entry for triangular correlation matrix (GH22840) (pandas-dev#23032)
1 parent 67a9115 commit dda017f

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

doc/source/cookbook.rst

+11
Original file line numberDiff line numberDiff line change
@@ -1226,6 +1226,17 @@ Computation
12261226
Correlation
12271227
***********
12281228

1229+
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:
1230+
1231+
.. ipython:: python
1232+
1233+
df = pd.DataFrame(np.random.random(size=(100, 5)))
1234+
1235+
corr_mat = df.corr()
1236+
mask = np.tril(np.ones_like(corr_mat, dtype=np.bool), k=-1)
1237+
1238+
corr_mat.where(mask)
1239+
12291240
The `method` argument within `DataFrame.corr` can accept a callable in addition to the named correlation types. Here we compute the `distance correlation <https://en.wikipedia.org/wiki/Distance_correlation>`__ matrix for a `DataFrame` object.
12301241

12311242
.. code-block:: python

0 commit comments

Comments
 (0)