polars.DataFrame.corr#

DataFrame.corr(
*,
label: str | None = None,
**kwargs: Any,
) DataFrame[source]#

Return pairwise Pearson product-moment correlation coefficients between columns.

See numpy corrcoef for more information: https://numpy.org/doc/stable/reference/generated/numpy.corrcoef.html

Parameters:
label

If given, a new column that contains the labels (column names) associated with each row is added, with this name.

**kwargs

Keyword arguments that are passed to numpy.corrcoef.

Notes

This functionality requires numpy to be installed.

Examples

>>> df = pl.DataFrame({"foo": [1, 2, 3], "bar": [3, 2, 1], "ham": [7, 8, 9]})
>>> df.corr()
shape: (3, 3)
┌──────┬──────┬──────┐
│ foo  ┆ bar  ┆ ham  │
│ ---  ┆ ---  ┆ ---  │
│ f64  ┆ f64  ┆ f64  │
╞══════╪══════╪══════╡
│ 1.0  ┆ -1.0 ┆ 1.0  │
│ -1.0 ┆ 1.0  ┆ -1.0 │
│ 1.0  ┆ -1.0 ┆ 1.0  │
└──────┴──────┴──────┘
>>> df.corr(label="cols")
shape: (3, 4)
┌──────┬──────┬──────┬──────┐
│ cols ┆ foo  ┆ bar  ┆ ham  │
│ ---  ┆ ---  ┆ ---  ┆ ---  │
│ str  ┆ f64  ┆ f64  ┆ f64  │
╞══════╪══════╪══════╪══════╡
│ foo  ┆ 1.0  ┆ -1.0 ┆ 1.0  │
│ bar  ┆ -1.0 ┆ 1.0  ┆ -1.0 │
│ ham  ┆ 1.0  ┆ -1.0 ┆ 1.0  │
└──────┴──────┴──────┴──────┘