polars.col#
创建一个表示DataFrame中列的表达。
col 在技术上不是一个函数,但它可以像函数一样使用。
请参阅下面的类文档以获取示例和更多文档。
- class polars.functions.col.Col[source]
创建Polars列表达式。
注释
此类的实例以名称
col导出。它可以像函数一样使用,例如通过调用pl.col("foo")。 有关更多文档,请参阅__call__()方法。这个辅助类通过属性查找提供了一种创建列表达式的替代语法。例如,
col.foo创建了一个等同于col("foo")的表达式。有关更多文档,请参见__getattr__()方法。函数调用语法被认为是构建列表达式的惯用方式。替代的属性语法对于快速原型设计可能很有用,因为它可以节省一些按键操作,但在表达性和可读性方面存在缺点。
示例
>>> from polars import col >>> df = pl.DataFrame( ... { ... "foo": [1, 2], ... "bar": [3, 4], ... } ... )
使用标准语法创建一个新的列表达式:
>>> df.with_columns(baz=(col("foo") * col("bar")) / 2) shape: (2, 3) ┌─────┬─────┬─────┐ │ foo ┆ bar ┆ baz │ │ --- ┆ --- ┆ --- │ │ i64 ┆ i64 ┆ f64 │ ╞═════╪═════╪═════╡ │ 1 ┆ 3 ┆ 1.5 │ │ 2 ┆ 4 ┆ 4.0 │ └─────┴─────┴─────┘
使用属性查找创建新的列表达式:
>>> df.with_columns(baz=(col.foo + col.bar)) shape: (2, 3) ┌─────┬─────┬─────┐ │ foo ┆ bar ┆ baz │ │ --- ┆ --- ┆ --- │ │ i64 ┆ i64 ┆ i64 │ ╞═════╪═════╪═════╡ │ 1 ┆ 3 ┆ 4 │ │ 2 ┆ 4 ┆ 6 │ └─────┴─────┴─────┘
方法:
__call__创建一个或多个表示DataFrame中列的表达式。
__getattr__使用属性语法创建一个列表达式。
- __call__(
- name: str | PolarsDataType | PythonDataType | Iterable[str] | Iterable[PolarsDataType | PythonDataType],
- *more_names: str | PolarsDataType | PythonDataType,
创建一个或多个表示DataFrame中列的表达式。
- Parameters:
- name
要表示的列的名称或数据类型。 接受正则表达式输入;正则表达式 应以
^开头并以$结尾。- *more_names
要表示的列的附加名称或数据类型, 指定为位置参数。
另请参阅
firstlastnth
示例
传递单个列名以表示该列。
>>> df = pl.DataFrame( ... { ... "ham": [1, 2], ... "hamburger": [11, 22], ... "foo": [2, 1], ... "bar": ["a", "b"], ... } ... ) >>> df.select(pl.col("foo")) shape: (2, 1) ┌─────┐ │ foo │ │ --- │ │ i64 │ ╞═════╡ │ 2 │ │ 1 │ └─────┘
使用点语法可以节省按键次数,快速进行原型设计。
>>> from polars import col as c >>> df.select(c.foo + c.ham) shape: (2, 1) ┌─────┐ │ foo │ │ --- │ │ i64 │ ╞═════╡ │ 3 │ │ 3 │ └─────┘
使用通配符
*来表示所有列。>>> df.select(pl.col("*")) shape: (2, 4) ┌─────┬───────────┬─────┬─────┐ │ ham ┆ hamburger ┆ foo ┆ bar │ │ --- ┆ --- ┆ --- ┆ --- │ │ i64 ┆ i64 ┆ i64 ┆ str │ ╞═════╪═══════════╪═════╪═════╡ │ 1 ┆ 11 ┆ 2 ┆ a │ │ 2 ┆ 22 ┆ 1 ┆ b │ └─────┴───────────┴─────┴─────┘ >>> df.select(pl.col("*").exclude("ham")) shape: (2, 3) ┌───────────┬─────┬─────┐ │ hamburger ┆ foo ┆ bar │ │ --- ┆ --- ┆ --- │ │ i64 ┆ i64 ┆ str │ ╞═══════════╪═════╪═════╡ │ 11 ┆ 2 ┆ a │ │ 22 ┆ 1 ┆ b │ └───────────┴─────┴─────┘
支持正则表达式输入。
>>> df.select(pl.col("^ham.*$")) shape: (2, 2) ┌─────┬───────────┐ │ ham ┆ hamburger │ │ --- ┆ --- │ │ i64 ┆ i64 │ ╞═════╪═══════════╡ │ 1 ┆ 11 │ │ 2 ┆ 22 │ └─────┴───────────┘
可以通过传递名称列表来表示多列。
>>> df.select(pl.col(["hamburger", "foo"])) shape: (2, 2) ┌───────────┬─────┐ │ hamburger ┆ foo │ │ --- ┆ --- │ │ i64 ┆ i64 │ ╞═══════════╪═════╡ │ 11 ┆ 2 │ │ 22 ┆ 1 │ └───────────┴─────┘
或者使用位置参数以相同的方式表示多个列。
>>> df.select(pl.col("hamburger", "foo")) shape: (2, 2) ┌───────────┬─────┐ │ hamburger ┆ foo │ │ --- ┆ --- │ │ i64 ┆ i64 │ ╞═══════════╪═════╡ │ 11 ┆ 2 │ │ 22 ┆ 1 │ └───────────┴─────┘
通过传递数据类型,轻松选择所有匹配特定数据类型的列。
>>> df.select(pl.col(pl.String)) shape: (2, 1) ┌─────┐ │ bar │ │ --- │ │ str │ ╞═════╡ │ a │ │ b │ └─────┘ >>> df.select(pl.col(pl.Int64, pl.Float64)) shape: (2, 3) ┌─────┬───────────┬─────┐ │ ham ┆ hamburger ┆ foo │ │ --- ┆ --- ┆ --- │ │ i64 ┆ i64 ┆ i64 │ ╞═════╪═══════════╪═════╡ │ 1 ┆ 11 ┆ 2 │ │ 2 ┆ 22 ┆ 1 │ └─────┴───────────┴─────┘
- __getattr__(name: str) Expr[source]
使用属性语法创建一个列表达式。
请注意,此语法不支持传递数据类型或多个列名。
- Parameters:
- name
要表示的列的名称。
示例
>>> from polars import col as c >>> df = pl.DataFrame( ... { ... "foo": [1, 2], ... "bar": [3, 4], ... } ... ) >>> df.select(c.foo + c.bar) shape: (2, 1) ┌─────┐ │ foo │ │ --- │ │ i64 │ ╞═════╡ │ 4 │ │ 6 │ └─────┘