使用极坐标时访问 .with_columns() 中新创建的列

k1n*_*ext 4 python python-polars

我是极地新手,不确定我是否.with_columns()正确使用。

这是我经常遇到的情况:有一个数据框,在其中.with_columns(),我对列应用一些操作。例如,我将一些日期转换为str类型date,然后想要计算开始日期和结束日期之间的持续时间。我将按如下方式实现这一点。

import polars as pl 

pl.DataFrame(
    {
        "start": ["01.01.2019", "01.01.2020"],
        "end": ["11.01.2019", "01.05.2020"],
    }
).with_columns(
    [
        pl.col("start").str.strptime(pl.Date, fmt="%d.%m.%Y"),
        pl.col("end").str.strptime(pl.Date, fmt="%d.%m.%Y"),
    ]
).with_columns(
    [
        (pl.col("end") - pl.col("start")).alias("duration"),
    ]
)
Run Code Online (Sandbox Code Playgroud)

首先,我转换两列,然后.with_columns()再次调用。

像这样更短的东西是行不通的:

pl.DataFrame(
    {
        "start": ["01.01.2019", "01.01.2020"],
        "end": ["11.01.2019", "01.05.2020"],
    }
).with_columns(
    [
        pl.col("start").str.strptime(pl.Date, fmt="%d.%m.%Y"),
        pl.col("end").str.strptime(pl.Date, fmt="%d.%m.%Y"),
        (pl.col("end") - pl.col("start")).alias("duration"),
    ]
)
Run Code Online (Sandbox Code Playgroud)

有没有办法避免调用.with_columns()两次并以更紧凑的方式编写?

jqu*_*ous 9

.with_columns需要第二个。

\n

来自@DeanMacGregor

\n
\n

详细地说,上下文中的所有内容(with_columns在本例中)只知道调用上下文之前数据帧中的内容。上下文中的每个表达式都不知道上下文中的每个其他表达式。这是设计使然,因为所有表达式都是并行运行的。如果您需要一个表达式来了解另一个表达式的输出,则需要另一个上下文。

\n
\n

您可以传递多个名称.col()并使用命名参数而不是.alias()

\n
(df\n .with_columns(\n    pl.col("start", "end").str.strptime(pl.Date, fmt="%d.%m.%Y"))\n .with_columns(\n    duration = pl.col("end") - pl.col("start")))\n
Run Code Online (Sandbox Code Playgroud)\n
shape: (2, 3)\n\xe2\x94\x8c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x90\n\xe2\x94\x82 start      | end        | duration     \xe2\x94\x82\n\xe2\x94\x82 ---        | ---        | ---          \xe2\x94\x82\n\xe2\x94\x82 date       | date       | duration[ms] \xe2\x94\x82\n\xe2\x95\x9e\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xaa\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xaa\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xa1\n\xe2\x94\x82 2019-01-01 | 2019-01-11 | 10d          \xe2\x94\x82\n\xe2\x94\x82 2020-01-01 | 2020-05-01 | 121d         \xe2\x94\x82\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xb4\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xb4\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x98\n
Run Code Online (Sandbox Code Playgroud)\n

  • @Thomas要详细说明,上下文中的所有内容(在本例中为“with_columns”)只知道调用上下文之前数据帧中的内容。上下文中的每个表达式都不知道上下文中的每个其他表达式。这是设计使然,因为所有表达式都是并行运行的。如果您需要一个表达式来了解另一个表达式的输出,则需要另一个上下文。 (2认同)