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
7890 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
"city": [
11
"New York",
12
"Los Angeles",
13
"Chicago",
14
"Houston",
15
"Phoenix",
16
"Amsterdam",
17
],
18
"country": ["USA", "USA", "USA", "USA", "USA", "Netherlands"],
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
"city": [
55
"New York",
56
"Los Angeles",
57
"Chicago",
58
"Houston",
59
"Amsterdam",
60
"Rotterdam",
61
"Utrecht",
62
],
63
"country": [
64
"USA",
65
"USA",
66
"USA",
67
"USA",
68
"Netherlands",
69
"Netherlands",
70
"Netherlands",
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 country, city, income, 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