polars.corr#
- polars.corr(a: str | Expr, b: str | Expr, *, method: CorrelationMethod = 'pearson', ddof: int = 1, propagate_nans: bool = False) Expr [source]#
Compute the pearson’s or spearman rank correlation correlation between two columns.
- Parameters:
- a
Column name or Expression.
- b
Column name or Expression.
- ddof
Delta degrees of freedom
- method{‘pearson’, ‘spearman’}
Correlation method.
- propagate_nans
If True any NaN encountered will lead to NaN in the output. Defaults to False where NaN 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 │ └─────┘