如何使用极坐标切割方法将结果返回到原始df

问题描述 投票:0回答:3

更新:

pl.cut
已从 Polars 中删除。添加了等价表达式:

.cut()
.qcut()


如何在选择上下文中使用它,例如 df.with_columns?

更具体地说,如果我有一个包含很多列的极坐标数据框,其中一列称为 x,我如何对 x 进行 pl.cut 并将分组结果附加到原始数据框中?

以下是我尝试过的方法,但不起作用:

df = pl.DataFrame({"a": [1, 2, 3, 4, 5], "b": [2, 3, 4, 5, 6], "x": [1, 3, 5, 7, 9]})
df.with_columns(pl.cut(pl.col("x"), bins=[2, 4, 6]))

非常感谢您的帮助。

python python-polars
3个回答
4
投票

docs 来看,截至 2023 年 1 月 25 日,

cut
接受一个 Series 并返回一个 DataFrame。 与许多/大多数方法和函数不同,它不需要表达式,因此您不能在
select
with_column(s)
中使用它。 为了得到你想要的结果,你必须将它加入到你原来的 df 中。

此外,

cut
似乎不一定保持与父系列相同的 dtypes。 (这肯定是一个错误)因此,在本例中,您必须将其转换回 int。

你会有:

df=df.join(
    pl.cut(df.get_column('x'),bins=[2,4,6]).with_column(pl.col('x').cast(pl.Int64())),
    on='x'
)

shape: (5, 5)
┌─────┬─────┬─────┬─────────────┬─────────────┐
│ a   ┆ b   ┆ x   ┆ break_point ┆ category    │
│ --- ┆ --- ┆ --- ┆ ---         ┆ ---         │
│ i64 ┆ i64 ┆ i64 ┆ f64         ┆ cat         │
╞═════╪═════╪═════╪═════════════╪═════════════╡
│ 1   ┆ 2   ┆ 1   ┆ 2.0         ┆ (-inf, 2.0] │
│ 2   ┆ 3   ┆ 3   ┆ 4.0         ┆ (2.0, 4.0]  │
│ 3   ┆ 4   ┆ 5   ┆ 6.0         ┆ (4.0, 6.0]  │
│ 4   ┆ 5   ┆ 7   ┆ inf         ┆ (6.0, inf]  │
│ 5   ┆ 6   ┆ 9   ┆ inf         ┆ (6.0, inf]  │
└─────┴─────┴─────┴─────────────┴─────────────┘

2
投票
df = pl.DataFrame(
    {"a": [1, 2, 3, 4, 5],
     "b": [2, 3, 4, 5, 6],
     "x": [1, 3, 5, 7, 9]}
)

df.with_columns(
    pl.col('x').cut([2, 4, 6]).alias('x_cut')
)
shape: (5, 4)
┌─────┬─────┬─────┬───────────┐
│ a   ┆ b   ┆ x   ┆ x_cut     │
│ --- ┆ --- ┆ --- ┆ ---       │
│ i64 ┆ i64 ┆ i64 ┆ cat       │
╞═════╪═════╪═════╪═══════════╡
│ 1   ┆ 2   ┆ 1   ┆ (-inf, 2] │
│ 2   ┆ 3   ┆ 3   ┆ (2, 4]    │
│ 3   ┆ 4   ┆ 5   ┆ (4, 6]    │
│ 4   ┆ 5   ┆ 7   ┆ (6, inf]  │
│ 5   ┆ 6   ┆ 9   ┆ (6, inf]  │
└─────┴─────┴─────┴───────────┘

旧解决方案

0.16.8
开始,顶级函数
pl.cut
已被弃用。您现在必须使用 series 方法
.cut
,它返回一个三列的 DataFrame。

# get x column as a Series and then apply .cut method
df['x'].cut(bins=[2, 4, 6])

它返回一个如下所示的 DataFrame:

shape: (5, 3)
┌─────┬─────────────┬─────────────┐
│ x   ┆ break_point ┆ category    │
│ --- ┆ ---         ┆ ---         │
│ f64 ┆ f64         ┆ cat         │
╞═════╪═════════════╪═════════════╡
│ 1.0 ┆ 2.0         ┆ (-inf, 2.0] │
│ 3.0 ┆ 4.0         ┆ (2.0, 4.0]  │
│ 5.0 ┆ 6.0         ┆ (4.0, 6.0]  │
│ 7.0 ┆ inf         ┆ (6.0, inf]  │
│ 9.0 ┆ inf         ┆ (6.0, inf]  │
└─────┴─────────────┴─────────────┘

如果您只想在主数据框中添加剪切类别。您可以直接在

with_columns()
中执行此操作:

df.with_columns(
    df['x'].cut(bins=[2, 4, 6], maintain_order=True)['category'].alias('x_cut')
)

# or
df.with_columns(
    x_cut=df['x'].cut(bins=[2, 4, 6], maintain_order=True)['category']
)
shape: (5, 4)
┌─────┬─────┬─────┬─────────────┐
│ a   ┆ b   ┆ x   ┆ x_cut       │
│ --- ┆ --- ┆ --- ┆ ---         │
│ i64 ┆ i64 ┆ i64 ┆ cat         │
╞═════╪═════╪═════╪═════════════╡
│ 1   ┆ 2   ┆ 1   ┆ (-inf, 2.0] │
│ 2   ┆ 3   ┆ 3   ┆ (2.0, 4.0]  │
│ 3   ┆ 4   ┆ 5   ┆ (4.0, 6.0]  │
│ 4   ┆ 5   ┆ 7   ┆ (6.0, inf]  │
│ 5   ┆ 6   ┆ 9   ┆ (6.0, inf]  │
└─────┴─────┴─────┴─────────────┘

1
投票

0.18.5
开始,您可以使用 cut 作为表达式。 (不幸的是,由于缺乏声誉,我无法将此作为对之前回复的评论)

import polars as pl
df = pl.DataFrame({"numbers": range(0, 20, 2)})
(
    df.with_columns(
        pl.col("numbers").cut([4, 7, 15]).alias("bins")
    )
)
© www.soinside.com 2019 - 2024. All rights reserved.