polars.corr#
- polars.corr(
- a: IntoExpr,
- b: IntoExpr,
- *,
- method: CorrelationMethod = 'pearson',
- ddof: int | None = None,
- propagate_nans: bool = False,
Compute the Pearson’s or Spearman rank correlation 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.
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 = pl.DataFrame( ... { ... "a": [1, 8, 3], ... "b": [4, 5, 2], ... "c": ["foo", "bar", "foo"], ... } ... ) >>> df.select(pl.corr("a", "b", method="spearman")) shape: (1, 1) ┌─────┐ │ a │ │ --- │ │ f64 │ ╞═════╡ │ 0.5 │ └─────┘