选择
在Polars SQL中,SELECT语句用于从表中检索数据到DataFrame中。Polars SQL中SELECT语句的基本语法如下:
SELECT column1, column2, ...
FROM table_name;
在这里,column1、column2等是您想从表中选择的列。您也可以使用通配符*来选择所有列。table_name是您想从中检索数据的表的名称。在下面的部分中,我们将介绍一些更常见的SELECT变体。
df = pl.DataFrame(
{
"city": [
"New York",
"Los Angeles",
"Chicago",
"Houston",
"Phoenix",
"Amsterdam",
],
"country": ["USA", "USA", "USA", "USA", "USA", "Netherlands"],
"population": [8399000, 3997000, 2705000, 2320000, 1680000, 900000],
}
)
ctx = pl.SQLContext(population=df, eager=True)
print(ctx.execute("SELECT * FROM population"))
shape: (6, 3)
┌─────────────┬─────────────┬────────────┐
│ city ┆ country ┆ population │
│ --- ┆ --- ┆ --- │
│ str ┆ str ┆ i64 │
╞═════════════╪═════════════╪════════════╡
│ New York ┆ USA ┆ 8399000 │
│ Los Angeles ┆ USA ┆ 3997000 │
│ Chicago ┆ USA ┆ 2705000 │
│ Houston ┆ USA ┆ 2320000 │
│ Phoenix ┆ USA ┆ 1680000 │
│ Amsterdam ┆ Netherlands ┆ 900000 │
└─────────────┴─────────────┴────────────┘
分组
GROUP BY 语句用于按一个或多个列对表中的行进行分组,并计算每个组的聚合函数。
result = ctx.execute(
"""
选择国家, 平均人口 as 平均人口
从人口
按国家分组
"""
)
打印(result)
shape: (2, 2)
┌─────────────┬────────────────┐
│ country ┆ avg_population │
│ --- ┆ --- │
│ str ┆ f64 │
╞═════════════╪════════════════╡
│ USA ┆ 3.8202e6 │
│ Netherlands ┆ 900000.0 │
└─────────────┴────────────────┘
排序
ORDER BY 语句用于按一个或多个列以升序或降序对查询结果集进行排序。
result = ctx.execute(
"""
选择城市, 人口
从人口
按人口排序
"""
)
打印(result)
shape: (6, 2)
┌─────────────┬────────────┐
│ city ┆ population │
│ --- ┆ --- │
│ str ┆ i64 │
╞═════════════╪════════════╡
│ Amsterdam ┆ 900000 │
│ Phoenix ┆ 1680000 │
│ Houston ┆ 2320000 │
│ Chicago ┆ 2705000 │
│ Los Angeles ┆ 3997000 │
│ New York ┆ 8399000 │
└─────────────┴────────────┘
连接
income = pl.DataFrame(
{
"city": [
"纽约",
"洛杉矶",
"芝加哥",
"休斯顿",
"阿姆斯特丹",
"鹿特丹",
"乌得勒支",
],
"country": [
"美国",
"美国",
"美国",
"美国",
"荷兰",
"荷兰",
"荷兰",
],
"income": [55000, 62000, 48000, 52000, 42000, 38000, 41000],
}
)
ctx.register_many(income=income)
result = ctx.execute(
"""
SELECT country, city, income, population
FROM population
LEFT JOIN income on population.city = income.city
"""
)
print(result)
shape: (6, 4)
┌─────────────┬─────────────┬────────┬────────────┐
│ country ┆ city ┆ income ┆ population │
│ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ str ┆ i64 ┆ i64 │
╞═════════════╪═════════════╪════════╪════════════╡
│ USA ┆ New York ┆ 55000 ┆ 8399000 │
│ USA ┆ Los Angeles ┆ 62000 ┆ 3997000 │
│ USA ┆ Chicago ┆ 48000 ┆ 2705000 │
│ USA ┆ Houston ┆ 52000 ┆ 2320000 │
│ USA ┆ Phoenix ┆ null ┆ 1680000 │
│ Netherlands ┆ Amsterdam ┆ 42000 ┆ 900000 │
└─────────────┴─────────────┴────────┴────────────┘
函数
Polars 提供了广泛的 SQL 函数,包括:
- 数学函数:
ABS,EXP,LOG,ASIN,ACOS,ATAN, 等。 - 字符串函数:
LOWER,UPPER,LTRIM,RTRIM,STARTS_WITH,ENDS_WITH. - 聚合函数:
SUM,AVG,MIN,MAX,COUNT,STDDEV,FIRST等。 - 数组函数:
EXPLODE,UNNEST,ARRAY_SUM,ARRAY_REVERSE, 等。
有关支持函数的完整列表,请访问 API文档。下面的示例 演示了如何在查询中使用函数
result = ctx.execute(
"""
选择城市, 人口
从人口
其中 STARTS_WITH(国家,'U')
"""
)
打印(result)
shape: (5, 2)
┌─────────────┬────────────┐
│ city ┆ population │
│ --- ┆ --- │
│ str ┆ i64 │
╞═════════════╪════════════╡
│ New York ┆ 8399000 │
│ Los Angeles ┆ 3997000 │
│ Chicago ┆ 2705000 │
│ Houston ┆ 2320000 │
│ Phoenix ┆ 1680000 │
└─────────────┴────────────┘
表函数
在之前的示例中,我们首先生成了一个DataFrame,并将其注册在SQLContext中。
Polars还支持在SQL查询中直接使用表函数read_xxx从CSV、Parquet、JSON和IPC读取数据。
result = ctx.execute(
"""
SELECT *
FROM read_csv('docs/assets/data/iris.csv')
"""
)
print(result)
shape: (150, 5)
┌──────────────┬─────────────┬──────────────┬─────────────┬───────────┐
│ sepal_length ┆ sepal_width ┆ petal_length ┆ petal_width ┆ species │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ f64 ┆ f64 ┆ f64 ┆ f64 ┆ str │
╞══════════════╪═════════════╪══════════════╪═════════════╪═══════════╡
│ 5.1 ┆ 3.5 ┆ 1.4 ┆ 0.2 ┆ Setosa │
│ 4.9 ┆ 3.0 ┆ 1.4 ┆ 0.2 ┆ Setosa │
│ 4.7 ┆ 3.2 ┆ 1.3 ┆ 0.2 ┆ Setosa │
│ 4.6 ┆ 3.1 ┆ 1.5 ┆ 0.2 ┆ Setosa │
│ 5.0 ┆ 3.6 ┆ 1.4 ┆ 0.2 ┆ Setosa │
│ … ┆ … ┆ … ┆ … ┆ … │
│ 6.7 ┆ 3.0 ┆ 5.2 ┆ 2.3 ┆ Virginica │
│ 6.3 ┆ 2.5 ┆ 5.0 ┆ 1.9 ┆ Virginica │
│ 6.5 ┆ 3.0 ┆ 5.2 ┆ 2.0 ┆ Virginica │
│ 6.2 ┆ 3.4 ┆ 5.4 ┆ 2.3 ┆ Virginica │
│ 5.9 ┆ 3.0 ┆ 5.1 ┆ 1.8 ┆ Virginica │
└──────────────┴─────────────┴──────────────┴─────────────┴───────────┘