Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/docs/source/src/python/user-guide/sql/select.py
8334 views
1
# --8<-- [start:setup]
2
import polars as pl
3
4
# --8<-- [end:setup]
5
6
7
# --8<-- [start:df]
8
df = pl.DataFrame(
9
{
10
"country": ["USA", "USA", "USA", "USA", "USA", "Netherlands"],
11
"city": [
12
"New York",
13
"Los Angeles",
14
"Chicago",
15
"Houston",
16
"Phoenix",
17
"Amsterdam",
18
],
19
"population": [8399000, 3997000, 2705000, 2320000, 1680000, 900000],
20
}
21
)
22
23
ctx = pl.SQLContext(population=df, eager=True)
24
25
print(ctx.execute("SELECT * FROM population"))
26
# --8<-- [end:df]
27
28
# --8<-- [start:group_by]
29
result = ctx.execute(
30
"""
31
SELECT country, AVG(population) as avg_population
32
FROM population
33
GROUP BY country
34
"""
35
)
36
print(result)
37
# --8<-- [end:group_by]
38
39
40
# --8<-- [start:orderby]
41
result = ctx.execute(
42
"""
43
SELECT city, population
44
FROM population
45
ORDER BY population
46
"""
47
)
48
print(result)
49
# --8<-- [end:orderby]
50
51
# --8<-- [start:join]
52
income = pl.DataFrame(
53
{
54
"country": [
55
"USA",
56
"USA",
57
"USA",
58
"USA",
59
"Netherlands",
60
"Netherlands",
61
"Netherlands",
62
],
63
"city": [
64
"New York",
65
"Los Angeles",
66
"Chicago",
67
"Houston",
68
"Amsterdam",
69
"Rotterdam",
70
"Utrecht",
71
],
72
"income": [55000, 62000, 48000, 52000, 42000, 38000, 41000],
73
}
74
)
75
ctx.register_many(income=income)
76
result = ctx.execute(
77
"""
78
SELECT income.*, population.population
79
FROM population
80
LEFT JOIN income ON population.city = income.city
81
"""
82
)
83
print(result)
84
# --8<-- [end:join]
85
86
87
# --8<-- [start:functions]
88
result = ctx.execute(
89
"""
90
SELECT city, population
91
FROM population
92
WHERE STARTS_WITH(country,'U')
93
"""
94
)
95
print(result)
96
# --8<-- [end:functions]
97
98
# --8<-- [start:tablefunctions]
99
result = ctx.execute(
100
"""
101
SELECT *
102
FROM read_csv('docs/assets/data/iris.csv')
103
"""
104
)
105
print(result)
106
# --8<-- [end:tablefunctions]
107
108