polars.corr#
- polars.corr(
- a: IntoExpr,
- b: IntoExpr,
- *,
- method: CorrelationMethod = 'pearson',
- ddof: int | None = None,
- propagate_nans: bool = False,
- eager: bool = False,
Compute the Pearson’s or Spearman rank correlation between two columns.
- Parameters:
- a
Column name or Expression.
- b
Column name or Expression.
- ddof
Has no effect, do not use.
Deprecated since version 1.17.0.
- method{‘pearson’, ‘spearman’}
Correlation method.
- propagate_nans
If
True
anyNaN
encountered will lead toNaN
in the output. Defaults toFalse
whereNaN
are regarded as larger than any finite number and thus lead to the highest rank.- eager
Evaluate immediately and return a
Series
; this requires that at least one of the given arguments is aSeries
. If set toFalse
(default), return an expression instead.
Examples
Pearson’s correlation:
>>> df = pl.DataFrame( ... { ... "a": [1, 8, 3], ... "b": [4, 5, 2], ... "c": ["foo", "bar", "foo"], ... } ... ) >>> df.select(pl.corr("a", "b")) shape: (1, 1) ┌──────────┐ │ a │ │ --- │ │ f64 │ ╞══════════╡ │ 0.544705 │ └──────────┘
Spearman rank correlation:
>>> df.select(pl.corr("a", "b", method="spearman")) shape: (1, 1) ┌─────┐ │ a │ │ --- │ │ f64 │ ╞═════╡ │ 0.5 │ └─────┘
Eager evaluation:
>>> s1 = pl.Series("a", [1, 8, 3]) >>> s2 = pl.Series("b", [4, 5, 2]) >>> pl.corr(s1, s2, eager=True) shape: (1,) Series: 'a' [f64] [ 0.544705 ] >>> pl.corr(s1, s2, method="spearman", eager=True) shape: (1,) Series: 'a' [f64] [ 0.5 ]