常用操作 
与许多其他数据框架库一样,Polars 提供了大量的常用函数来对 Dataframe 进行操作。 熟悉 Dataframes 的用户会发现 Polars 与 Pandas 或 R 的实现有许多相似之处。
添加列 
py
from .dataset import df
import polars as pl
out = df.with_columns(pl.Series(["p", "q", "r", "s", "t"]).alias("e"))  # .alias方法增加一列
print(out)txt
shape: (5, 5)
┌──────┬──────┬──────────┬─────┬─────┐
│ a    ┆ b    ┆ c        ┆ d   ┆ e   │
│ ---  ┆ ---  ┆ ---      ┆ --- ┆ --- │
│ i64  ┆ str  ┆ f64      ┆ str ┆ str │
╞══════╪══════╪══════════╪═════╪═════╡
│ 1    ┆ foo  ┆ 0.37454  ┆ a   ┆ p   │
│ 2    ┆ ham  ┆ 0.950714 ┆ b   ┆ q   │
│ 3    ┆ spam ┆ 0.731994 ┆ c   ┆ r   │
│ null ┆ egg  ┆ 0.598658 ┆ d   ┆ s   │
│ 5    ┆ null ┆ 0.156019 ┆ e   ┆ t   │
└──────┴──────┴──────────┴─────┴─────┘类型转换 
这个例子使用的是 Python 数据类型,但我们也可以在 Polars dtypes (如 pl.Float32、pl.Float64)之间进行转换。
py
out = df.with_columns(pl.col("a").cast(float))
print(out)txt
shape: (5, 4)
┌──────┬──────┬──────────┬─────┐
│ a    ┆ b    ┆ c        ┆ d   │
│ ---  ┆ ---  ┆ ---      ┆ --- │
│ f64  ┆ str  ┆ f64      ┆ str │
╞══════╪══════╪══════════╪═════╡
│ 1.0  ┆ foo  ┆ 0.37454  ┆ a   │
│ 2.0  ┆ ham  ┆ 0.950714 ┆ b   │
│ 3.0  ┆ spam ┆ 0.731994 ┆ c   │
│ null ┆ egg  ┆ 0.598658 ┆ d   │
│ 5.0  ┆ null ┆ 0.156019 ┆ e   │
└──────┴──────┴──────────┴─────┘重命名列 
py
import numpy as np
import polars as pl
df = pl.DataFrame(
    {
        "a": [1, 2, 3, None, 5],
        "b": ["foo", "ham", "spam", "egg", None],
        "c": np.random.rand(5),
        "d": ["a", "b", "c", "d", "e"],
    }
)
df.columns = ["banana", "orange", "apple", "grapefruit"]  # 重命名列txt
shape: (5, 4)
┌────────┬────────┬──────────┬────────────┐
│ banana ┆ orange ┆ apple    ┆ grapefruit │
│ ---    ┆ ---    ┆ ---      ┆ ---        │
│ i64    ┆ str    ┆ f64      ┆ str        │
╞════════╪════════╪══════════╪════════════╡
│ 1      ┆ foo    ┆ 0.155995 ┆ a          │
│ 2      ┆ ham    ┆ 0.058084 ┆ b          │
│ 3      ┆ spam   ┆ 0.866176 ┆ c          │
│ null   ┆ egg    ┆ 0.601115 ┆ d          │
│ 5      ┆ null   ┆ 0.708073 ┆ e          │
└────────┴────────┴──────────┴────────────┘删除列 
py
from .dataset import df
import polars as pl
# 删除单独的列
out = df.drop("d")
# 删除多个列
out = df.drop(["b", "c"])
# 选择所有列但是不包括('b', 'c')
out = df.select(pl.all().exclude(["b", "c"]))
# 仅选择列"a"
out = df.select(pl.col("a"))txt
shape: (5, 1)
┌──────┐
│ a    │
│ ---  │
│ i64  │
╞══════╡
│ 1    │
│ 2    │
│ 3    │
│ null │
│ 5    │
└──────┘删除空值 
python
df.drop_nulls()txt
shape: (3, 4)
┌─────┬──────┬──────────┬─────┐
│ a   ┆ b    ┆ c        ┆ d   │
│ --- ┆ ---  ┆ ---      ┆ --- │
│ i64 ┆ str  ┆ f64      ┆ str │
╞═════╪══════╪══════════╪═════╡
│ 1   ┆ foo  ┆ 0.37454  ┆ a   │
│ 2   ┆ ham  ┆ 0.950714 ┆ b   │
│ 3   ┆ spam ┆ 0.731994 ┆ c   │
└─────┴──────┴──────────┴─────┘填充缺失值(NA) 
策略:
- mean:平均值
- backward:上一值
- min:最小值
- max:最大值
python
df.fill_none("forward")txt
shape: (5, 4)
┌─────┬──────┬──────────┬─────┐
│ a   ┆ b    ┆ c        ┆ d   │
│ --- ┆ ---  ┆ ---      ┆ --- │
│ i64 ┆ str  ┆ f64      ┆ str │
╞═════╪══════╪══════════╪═════╡
│ 1   ┆ foo  ┆ 0.37454  ┆ a   │
│ 2   ┆ ham  ┆ 0.950714 ┆ b   │
│ 3   ┆ spam ┆ 0.731994 ┆ c   │
│ 3   ┆ egg  ┆ 0.598658 ┆ d   │
│ 5   ┆ egg  ┆ 0.156019 ┆ e   │
└─────┴──────┴──────────┴─────┘获取所有列 
python
df.columnstext
['a', 'b', 'c', 'd']空值计数 
python
df.null_count()txt
shape: (1, 4)
┌─────┬─────┬─────┬─────┐
│ a   ┆ b   ┆ c   ┆ d   │
│ --- ┆ --- ┆ --- ┆ --- │
│ u32 ┆ u32 ┆ u32 ┆ u32 │
╞═════╪═════╪═════╪═════╡
│ 1   ┆ 1   ┆ 0   ┆ 0   │
└─────┴─────┴─────┴─────┘列排序 
python
df.sort("a", reverse=True)txt
shape: (5, 4)
┌──────┬──────┬──────────┬─────┐
│ a    ┆ b    ┆ c        ┆ d   │
│ ---  ┆ ---  ┆ ---      ┆ --- │
│ i64  ┆ str  ┆ f64      ┆ str │
╞══════╪══════╪══════════╪═════╡
│ null ┆ egg  ┆ 0.598658 ┆ d   │
│ 5    ┆ null ┆ 0.156019 ┆ e   │
│ 3    ┆ spam ┆ 0.731994 ┆ c   │
│ 2    ┆ ham  ┆ 0.950714 ┆ b   │
│ 1    ┆ foo  ┆ 0.37454  ┆ a   │
└──────┴──────┴──────────┴─────┘转为 NumPy 
python
df.to_numpy()text
[[1.0 'foo' 0.3745401188473625 'a']
 [2.0 'ham' 0.9507143064099162 'b']
 [3.0 'spam' 0.7319939418114051 'c']
 [nan 'egg' 0.5986584841970366 'd']
 [5.0 None 0.15601864044243652 'e']]转为 Pandas 
python
df.to_pandas()text
     a     b         c  d
0  1.0   foo  0.374540  a
1  2.0   ham  0.950714  b
2  3.0  spam  0.731994  c
3  NaN   egg  0.598658  d
4  5.0  None  0.156019  e